R - log1p() Function
The R log1p() function returns the natural logarithm of (1 + number), i.e., log(1+number). In special cases it returns the following:
- If the argument is NaN or less than -1, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is -1, then the result is negative infinity.
Syntax
log1p(x)
Parameters
x |
Required. Specify column to compute on. |
Return Value
Returns the base-10 logarithm of the value.
Example:
In the example below, log1p() function is used to calculate the base-10 logarithm of a given number.
#operating on single element atomic vector print(log1p(-1)) print(log1p(0.5)) print(log1p(1)) cat("\nOperating on vector\n") #operating on vector v <- c(0, 10, 50) print(log1p(v)) cat("\nOperating on matrix\n") #operating on matrix m <- matrix(c(0, 10, 50, 100, 500, NaN), nrow=2) print(log1p(m)) cat("\nOperating on first column of matrix\n") #operating on first column of matrix print(log1p(m[,1]))
The output of the above code will be:
[1] -Inf [1] 0 [1] 1.098612 Operating on vector [1] 1.609438 2.302585 3.912023 Operating on matrix [,1] [,2] [,3] [1,] 0.000000 3.912023 6.214608 [2,] 2.302585 4.605170 NaN Operating on first column of matrix [1] 0.000000 2.302585
❮ R Math Functions