Numeric functions

There are many numeric functions in R that you might find beneficial based on your specific use cases and the nature of your data. Here are some of them categorized based on their functionality.

In order to demonstrate these functions, we will create a small table:

my_table <- 
  tibble(val = runif(n = 10, min = -2, max = 2)) |> 
  arrange(val)
my_table
# A tibble: 10 × 1
       val
     <dbl>
 1 -1.46  
 2 -0.855 
 3  0.0764
 4  0.567 
 5  0.628 
 6  0.820 
 7  0.946 
 8  1.32  
 9  1.66  
10  1.75  

1 Basic Arithmetic Operations

  • abs(x): Returns the absolute value of x.
  • sqrt(x): Returns the square root of x.
  • ceiling(x): Returns the smallest integer not less than x.
  • floor(x): Returns the largest integer not greater than x.
  • trunc(x): Returns the truncated value of x.
  • round(x, digits): Rounds the values of x to the specified number of decimal places.
my_table |> 
  mutate(
    abs_val = abs(val),
    sqrt_val = sqrt(val),
    ceiling_val = ceiling(val),
    floor_val = floor(val),
    trunc_val = trunc(val),
    round_val = round(val, 1)
  )
# A tibble: 10 × 7
       val abs_val sqrt_val ceiling_val floor_val trunc_val round_val
     <dbl>   <dbl>    <dbl>       <dbl>     <dbl>     <dbl>     <dbl>
 1 -1.46    1.46    NaN              -1        -2        -1      -1.5
 2 -0.855   0.855   NaN               0        -1         0      -0.9
 3  0.0764  0.0764    0.276           1         0         0       0.1
 4  0.567   0.567     0.753           1         0         0       0.6
 5  0.628   0.628     0.792           1         0         0       0.6
 6  0.820   0.820     0.906           1         0         0       0.8
 7  0.946   0.946     0.973           1         0         0       0.9
 8  1.32    1.32      1.15            2         1         1       1.3
 9  1.66    1.66      1.29            2         1         1       1.7
10  1.75    1.75      1.32            2         1         1       1.7
my_table |> 
  mutate(abs_val = abs(val),
         sqrt_val = sqrt(val),
         ceiling_val = ceiling(val),
         floor_val = floor(val),
         trunc_val = trunc(val),
         round_val = round(val, 1)
         ) |> 
  pivot_longer(cols = ends_with("_val"), 
               names_to = "operation", 
               values_to = "output") |> 
  mutate(operation = str_remove(operation, "_val"))
# A tibble: 60 × 3
      val operation  output
    <dbl> <chr>       <dbl>
 1 -1.46  abs         1.46 
 2 -1.46  sqrt      NaN    
 3 -1.46  ceiling    -1    
 4 -1.46  floor      -2    
 5 -1.46  trunc      -1    
 6 -1.46  round      -1.5  
 7 -0.855 abs         0.855
 8 -0.855 sqrt      NaN    
 9 -0.855 ceiling     0    
10 -0.855 floor      -1    
# ℹ 50 more rows

2 Trigonometric Functions

  • sin(x), cos(x), tan(x): Return the sine, cosine, and tangent of x, respectively.
  • asin(x), acos(x), atan(x): Return the inverse sine, cosine, and tangent of x, respectively.
my_table |> 
  mutate(sin_val = sin(val),
         cos_val = cos(val),
         tan_val = tan(val),
         asin_val = asin(val),
         acos_val = acos(val),
         atan_val = atan(val))
# A tibble: 10 × 7
       val sin_val cos_val  tan_val asin_val acos_val atan_val
     <dbl>   <dbl>   <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
 1 -1.46   -0.994   0.109   -9.10   NaN       NaN      -0.971 
 2 -0.855  -0.755   0.656   -1.15    -1.03      2.60   -0.708 
 3  0.0764  0.0763  0.997    0.0765   0.0765    1.49    0.0762
 4  0.567   0.537   0.844    0.637    0.603     0.968   0.516 
 5  0.628   0.588   0.809    0.726    0.679     0.892   0.561 
 6  0.820   0.731   0.682    1.07     0.962     0.609   0.687 
 7  0.946   0.811   0.585    1.39     1.24      0.329   0.758 
 8  1.32    0.969   0.246    3.93   NaN       NaN       0.923 
 9  1.66    0.996  -0.0883 -11.3    NaN       NaN       1.03  
10  1.75    0.984  -0.177   -5.57   NaN       NaN       1.05  

3 Exponential and Logarithmic Functions

  • exp(x): Calculates the exponential of x.
  • log(x, base): Computes logarithms with base base. Default is the natural logarithm.
  • log10(x): Computes base 10 logarithms.
my_table |> 
  mutate(exp_val = exp(val),
         loge_val = log(val),
         log2_val = log(val, 2),
         log10_val = log10(val))
# A tibble: 10 × 5
       val exp_val loge_val log2_val log10_val
     <dbl>   <dbl>    <dbl>    <dbl>     <dbl>
 1 -1.46     0.232 NaN      NaN       NaN     
 2 -0.855    0.425 NaN      NaN       NaN     
 3  0.0764   1.08   -2.57    -3.71     -1.12  
 4  0.567    1.76   -0.567   -0.819    -0.246 
 5  0.628    1.87   -0.465   -0.671    -0.202 
 6  0.820    2.27   -0.198   -0.286    -0.0860
 7  0.946    2.58   -0.0551  -0.0795   -0.0239
 8  1.32     3.75    0.279    0.402     0.121 
 9  1.66     5.26    0.506    0.731     0.220 
10  1.75     5.74    0.559    0.806     0.243 

4 Statistical Functions

  • scale(x): Centers and scales a numeric vector.
  • quantile(x, probs): Calculates quantiles from a numeric vector.
  • rank(x): Returns the sample ranks of the values in a vector.
  • ecdf(x): Computes empirical cumulative distribution functions.
my_table |> 
  mutate(scale_val = scale(val))
# A tibble: 10 × 2
       val scale_val[,1]
     <dbl>         <dbl>
 1 -1.46         -1.93  
 2 -0.855        -1.35  
 3  0.0764       -0.450 
 4  0.567         0.0211
 5  0.628         0.0797
 6  0.820         0.265 
 7  0.946         0.386 
 8  1.32          0.747 
 9  1.66          1.07  
10  1.75          1.16  
quantile(my_table$val)
        0%        25%        50%        75%       100% 
-1.4613336  0.1990334  0.7241141  1.2279312  1.7483017 
rank(my_table$val)
 [1]  1  2  3  4  5  6  7  8  9 10
ecdf(my_table$val)
Empirical CDF 
Call: ecdf(my_table$val)
 x[1:10] = -1.4613, -0.85544, 0.076384,  ..., 1.6592, 1.7483

5 Random Number Generation

  • runif(n): Generates n uniform random numbers.
  • rnorm(n): Generates n normal random numbers.
  • sample(x, size): Randomly samples elements from a vector.
runif(10, min = 3, max = 7)
 [1] 4.830967 5.876449 6.738689 4.021715 4.849171 6.760058 6.912906 3.469949
 [9] 4.899988 5.241331
rnorm(10, mean = 100, sd = 10)
 [1] 113.04870 122.86645  86.11139  97.21211  98.66679 106.35950  97.15747
 [8]  73.43545  75.59533 113.20113
xvals = rnorm(1000, mean = 1000, sd = 300)
sample(xvals, 10)
 [1] 1305.0618  987.4085 1159.8462 1363.2729 1016.6461  312.1086 1371.7452
 [8]  573.2997 1317.9478 1259.4334

6 Miscellaneous

  • cumsum(x): Computes the cumulative sum of elements.
  • cumprod(x): Computes the cumulative product of elements.
  • diff(x): Computes differences between subsequent elements.
  • pmin(x, y), pmax(x, y): Element-wise minima and maxima of two vectors.
vals1 <- rnorm(3, mean = 100, sd = 10)
print(vals1)
[1] 103.88439  91.55107 107.37990
vals2 <- rnorm(3, mean = 100, sd = 10)
print(vals2)
[1]  89.20240  89.73526 102.88793
cumsum(vals1)
[1] 103.8844 195.4355 302.8154
cumprod(vals1)
[1]     103.8844    9510.7269 1021260.9342
diff(vals1)
[1] -12.33332  15.82884
pmin(vals1, vals2)
[1]  89.20240  89.73526 102.88793
pmax(vals1, vals2)
[1] 103.88439  91.55107 107.37990

7 Working with NA Values

  • is.na(x): Checks for missing values.
  • na.omit(x): Returns the object with incomplete cases removed.
vals <- rnorm(5, mean = 0, sd = 10)
print(vals)
[1]  0.9081132  2.6262318  0.6933491 -5.2864382 -1.3020216
vals <- sqrt(vals)
print(vals)
[1] 0.9529497 1.6205653 0.8326759       NaN       NaN
is.na(vals)
[1] FALSE FALSE FALSE  TRUE  TRUE
as.numeric(na.omit(vals))
[1] 0.9529497 1.6205653 0.8326759

8 Vectorized if-else

  • ifelse(test, yes, no): Returns a value with the same shape as test where each element is either yes or no based on the condition.
vals <- rnorm(10, mean = 0, sd = 10)
print(vals)
 [1]  16.2015874  -0.1789512 -13.1848940  -8.4456016 -11.0181487  -9.0009014
 [7] -12.6108392 -26.2584890   6.6906575   6.6004151
ifelse(vals > 0, 3, -1)
 [1]  3 -1 -1 -1 -1 -1 -1 -1  3  3

Each of these functions has its role and application, and using them effectively can enhance your data manipulation and analytical capabilities in R.