Like other programming languages, R programming language also has various built-in mathematical functions to perform mathematical calculations. All these functions are vectorised. In this tutorial you will learn what are the built-in mathematical functions in R and how to use mathematical functions in R.
Built-in Mathematical Functions in R
Some commonly used built-in mathematical functions in R are as follows:
Function | Operation Performed |
---|---|
max(x) |
Maximum of the elements of x |
min(x) |
Minimum of the elements of x |
sqrt(x) |
Square root of x |
abs(x) |
Absolute value of x |
exp(x) |
Exponential value of x |
log(x,base) |
Logarithm value of x with given base |
log10(x) |
Logarithm of x (base 10) |
ceiling(x) |
Closest integer not less than x |
floor(x) |
Closest integer not greater than x |
trunc(x) |
Closest integer towards zero |
round(x,n) |
Round x to n significant places |
rev(x) |
reverse the elements of x |
sort(x) |
sort the elements of x in increasing order |
rank(x) |
assign ranks to the elements of x |
Examples of Some Mathematical Functions
min() function in R
The min()
function returns the minimum of the elements of a vector.
# Create a vector x
x <- c(10,-20,45,30)
y <- c(10,-20,45,30,NA)
min(x)
[1] -20
min(y)
[1] NA
max() function in R
The max()
function returns the maximum of the elements of a vector.
# Create a vector x
x <- c(10,-20,45,30)
y <- c(10,-20,45,30,NA)
max(x)
[1] 45
max(y,na.rm = TRUE)
[1] 45
Note that while using max
or min
function, if the vector contains NA
values the result will be NA
because the default value of argument na.rm=FALSE
. To remove the missing values while computing min
or max
use the argument na.rm=TRUE
.
abs() function in R
The abs()
function returns the absolute values of the elements of a vector.
# Create a vector x
x <- c(10,-20,45,30)
# Compute absolute of x
abs(x)
[1] 10 20 45 30
sqrt() function in R
The sqrt()
function returns the principal square root of the elements of a vector.
# compute square root of x
sqrt(x)
Warning in sqrt(x): NaNs produced
[1] 3.162278 NaN 6.708204 5.477226
Examples of logarithmic and exponent functions in R
log() function in R
The log()
function returns the natural logarithm of the elements of a vector.
# Create a vector y
y <- c(5,6.5,8)
# compute natural logarithm of y
log(y)
[1] 1.609438 1.871802 2.079442
Note that in log(x,base)
function, if you do not specify the base
argument, R will use the default base as e
(i.e., logarithm of x
to the base e
).
Suppose we need to find the logarithm of elements of y
vector with base 2
, we can use same log()
function with argument base=2
.
log(y,base=2)
[1] 2.321928 2.700440 3.000000
log10() function in R
The log10()
function returns the logarithm of the elements of a vector to the base 10.
# compute logarithm of y to the base 10
log10(y)
[1] 0.6989700 0.8129134 0.9030900
exp() function in R
The exp()
function returns the exponential of the elements of a vector.
# compute exponent of y
exp(y)
[1] 148.4132 665.1416 2980.9580
Examples of Rounding functions in R
R has number of rounding functions available in the base
package like ceiling()
, floor()
, trunc()
, round()
and signif()
.
# Create a vector x
x<-c(10.6787,9.456,12.554,-10.67)
ceiling() function in R
The ceiling(x)
function is used to return the closest integer not less than the corresponding elements of x
.
# return closest integer not less than x
ceiling(x)
[1] 11 10 13 -10
floor() function in R
The floor(x)
function is used to return the closest integer not greater than the corresponding elements of x
.
# return closest integer not greater than x
floor(x)
[1] 10 9 12 -11
trunc() function in R
The trunc(x)
function returns a numeric vector containing the integers formed by truncating the values in x
towards zero.
trunc(x)
[1] 10 9 12 -10
round() function in R
The round(x,digits)
function is used to return the rounded values to the specified number of decimal places.
# round x to 2 significant places
round(x,digits=2)
[1] 10.68 9.46 12.55 -10.67
signif() function in R
The signif(x,digits)
function rounds the values in its first argument to the specific number of significant digits.
g <- exp(-10:-7)
g
[1] 4.539993e-05 1.234098e-04 3.354626e-04 9.118820e-04
signif(g,digits = 3)
[1] 4.54e-05 1.23e-04 3.35e-04 9.12e-04
Examples of rev, rank and sort function in R
rev() function in R
The rev(x)
function is used to get the vector x
in reverse order, i.e., the rev(x)
function reverse the elements of the vector x
.
# create a vector x
x <- c(10,12,9,13,8,17,13)
# reverse the elements of x
rev(x)
[1] 13 17 8 13 9 12 10
rank() function in R
The rank()
function is used to assign the ranks to the values in a vector. The ranks are given from smallest to largest. In case of ties in the observations, the ranks are given as an average rank they jointly occupy (by default R uses ties.method="average"
).
x
[1] 10 12 9 13 8 17 13
# assign ranks to the elements of x
avg_ties <- rank(x)
avg_ties
[1] 3.0 4.0 2.0 5.5 1.0 7.0 5.5
Assign the rank 1 to the smallest value 8
, the rank 2 to the next value 9
and so on. Since the value 13 occurs twice, R assign the average rank which they jointly occupy (i.e., $\frac{5+6}{2}=5.5$).
For other ties.method
see the R code and its output.
first_ties <- rank(x,ties.method="first")
first_ties
[1] 3 4 2 5 1 7 6
The first value of the rank (which they jointly occupy) is assigned to the values when ties encountered.
last_ties <- rank(x,ties.method="last")
last_ties
[1] 3 4 2 6 1 7 5
The last value of the rank (which they jointly occupy) is assigned to the values when ties encountered.
random_ties <- rank(x,ties.method="random")
random_ties
[1] 3 4 2 5 1 7 6
The random value of the rank (which they jointly occupy) is assigned to the values when ties encountered.
max_ties <- rank(x,ties.method="max")
max_ties
[1] 3 4 2 6 1 7 6
The maximum value of the rank (which they jointly occupy) is assigned to the values when ties encountered.
min_ties <- rank(x,ties.method="min")
min_ties
[1] 3 4 2 5 1 7 5
The minimum` value of the rank (which they jointly occupy) is assigned to the values when ties encountered.
Ranks by different ties.method
for comparison
x | avg_ties | first_ties | last_ties | random_ties | max_ties | min_ties |
---|---|---|---|---|---|---|
10 | 3.0 | 3 | 3 | 3 | 3 | 3 |
12 | 4.0 | 4 | 4 | 4 | 4 | 4 |
9 | 2.0 | 2 | 2 | 2 | 2 | 2 |
13 | 5.5 | 5 | 6 | 5 | 6 | 5 |
8 | 1.0 | 1 | 1 | 1 | 1 | 1 |
17 | 7.0 | 7 | 7 | 7 | 7 | 7 |
13 | 5.5 | 6 | 5 | 6 | 6 | 5 |
rank() function in R with NA
If the vector contains NA
values, then by default NA
values are ranked last (default argument is na.last=TRUE
).
data_1 <- c(10,12,9,13,NA,8,17,13)
rank(data_1)
[1] 3.0 4.0 2.0 5.5 8.0 1.0 7.0 5.5
To assign NA
the first rank, use the argument na.last=FALSE
.
rank(data_1,na.last = FALSE)
[1] 4.0 5.0 3.0 6.5 1.0 2.0 8.0 6.5
The NA
values can be removed while ranking using the argument na.last=NA
.
rank(data_1,na.last=NA)
[1] 3.0 4.0 2.0 5.5 1.0 7.0 5.5
sort() function in R
The sort()
function sorts the elements of vector in increasing order of magnitude by default. To sort the elements in decreasing order, use the argument decreasing=TRUE
in sort()
function.
# sort elements of x in increasing order
sort(x)
[1] 8 9 10 12 13 13 17
# sort elements of x in decreasing order
sort(x,decreasing=TRUE)
[1] 17 13 13 12 10 9 8
If the data contains NA
value(s), by default the sort function ignore the NA
value(s) and sort the remaining data.
sort(data_1)
[1] 8 9 10 12 13 13 17
Use the argument na.last=TRUE
to put the NA
values last while sorting.
sort(data_1,na.last=TRUE)
[1] 8 9 10 12 13 13 17 NA
Use the argument na.last=FALSE
to put the NA
values first while sorting.
sort(data_1,na.last=FALSE)
[1] NA 8 9 10 12 13 13 17
Build-in Functions for Complex numbers
Some built-in function for complex numbers in R are as follows:
Function | Operation Performed |
---|---|
Re(x) |
Real part of complex number x |
Im(x) |
Imaginary part of complex number x |
Mod(x) |
Modulus of complex number x |
abs(x) |
Modulus of complex number x |
Arg(x) |
Angle in radian of complex number x |
Conj(x) |
Complex conjugate of complex number x |
Examples of Built-in function for complex numbers
Suppose the given complex number is z=3+4i
.
# Store a complex number to z
z<-3+4i
Re() or Im() function for complex numbers
For the complex number $z=a+bi$, the real part of $z$ is $a$ and the imaginary part of $z$ is $b$. The function Re(z)
and Im(z)
returns real and imaginary part of $z$ respectively.
## Returns real part of z
Re(z)
[1] 3
## Returns imaginary part of z
Im(z)
[1] 4
Mod() or abs() function for complex numbers
To get the modulus of complex number, use the function Mod(z)
or abs(z)
. The modulus of complex number $z=a+bi$ is given by $|z|=\sqrt{a^2+b^2}$.
## Returns modulus of z
Mod(z)
[1] 5
## Returns modulus of z
abs(z)
[1] 5
Arg() function for complex numbers
The argument of complex number $z=a+bi$ is the angle made by the complex number $z$ with the positive real axis. To get the real part of complex number, use the function Re(z)
.
## Returns angle in radians of z
Arg(z)
[1] 0.9272952
conj() function for complex numbers
The conjugate of the complex number $z=a+bi$ is $\overline{z}=a-bi$. To get the complex conjugate of the complex number, use the function Conj(z)
.
## Return complex conjugate of z
Conj(z)
[1] 3-4i
Endnote
In this tutorial you learned about some commonly used built-in mathematical functions in R and how to use these functions in R.
To learn more about other built-in functions and user-defined functions in R, please refer to the following tutorials:
- Built-in Special Mathematical functions in R
- Built-in Trigonometric functions in R
- Built-in Statistical functions in R
- Built-in Character functions in R
- User-defined functions in R Part I
- User-defined functions in R Part II
- Functions in R
Hopefully you enjoyed learning this tutorial on built-in mathematical functions in R. Hope the content is more than sufficient to understand mathematical functions in R.