Performing Logarithmic Computations in R Programming - log(), log10(), log1p(), and log2() Functions - GeeksforGeeks

05 Jun, 2020
log() function in R Language returns the natural logarithm (base-e logarithm) of the argument passed in the parameter.
Syntax: log(x)
Parameter:x: Specified value.
Returns: It returns the natural logarithm of the specified value, infinity for 0 and NaN for the negative value.
Example 1:
# R program to calculate log value print(log(1)) print(log(30)) print(log(0)) print(log(-44)) 
Output:
[1] 0
[1] 3.401197
[1] -Inf
[1] NaN
Warning message:
In log(-44) : NaNs produced

The log(x,base=y) is an inbuilt function in R which is used to compute the logarithm of the specified value to base y, infinity for 0, and NaN for the negative value.
Syntax: log(x, base = y)
Parameters:x and base y.
Returns: It returns the logarithm of the specified value to base y, infinity for 0, and NaN for the negative value.
Example 2:
# R program to calculate log value print(log(10,base=10)) print(log(16,base=2)) print(log(0,base=10)) print(log(-44,base=4)) 
Output:
[1] 1
[1] 4
[1] -Inf
[1] NaN
Warning message:
In print(log(-44, base = 4)) : NaNs produced

The log10() is an inbuilt function in R which is used to compute the logarithm of the specified value to base 10, infinity for 0, and NaN for the negative value.
Syntax: log10(x)
Parameters:x: Specified values.
Returns: It returns the logarithm of the specified value to base 10,infinity for 0 and NaN for negative value.
Example 2:
# R program to calculate log value print(log10(1)) print(log10(10)) print(log10(0)) print(log10(-44)) 
Output:
[1] 0
[1] 1
[1] -Inf
[1] NaN
Warning message:
In print(log10(-44)) : NaNs produced

The log1p() is an inbuilt function in R which is used to calculate accurate natural logarithm of 1+x, where x is the specified value and throws infinity for 0 and NaN for negative value.
Syntax: log1p(x)
Parameters:x: Specified values.
Returns: It returns the accurate natural logarithm of 1+x, where x is the specified value and throws infinity for 0 and NaN for negative value.
Example:
# R program to illustrate # the use of log1p() method  # Getting the accurate natural # logarithm of 1 + x, where x is # the specified value and throws # infinity for 0 and NaN for negative value.print(log1p(1))print(log1p(10))print(log1p(0))print(log1p(-44))
Output:
[1] 0.6931472
[1] 2.397895
[1] 0
[1] NaN
Warning message:
In log1p(-44) : NaNs produced

The log2() is an inbuilt function in R which is used to calculate the logarithm of x to base 2, where x is the specified value or throws infinity for 0 and NaN for negative value.
Syntax: log2(x)
Parameters:x: Specified values.
Returns: It returns the logarithm of x to base 2, where x is the specified value or throws infinity for 0 and NaN for negative value.
Example:
# R program to illustrate # the use of log2() method  # Getting the logarithm of x # to base 2, where x is the # specified value or throws # infinity for 0 and NaN for negative value.print(log2(1))print(log2(2))print(log2(0))print(log2(-44))
Output:
[1] 0
[1] 1
[1] -Inf
[1] NaN
Warning message:
In print(log2(-44)) : NaNs produced

R Math-Function
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Replace specific values in column in R DataFrame ?
How to change Row Names of DataFrame in R ?
Filter data by multiple conditions in R using Dplyr
Change Color of Bars in Barchart using ggplot2 in R
Loops in R (for, while, repeat)
Printing Output of an R Program
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
K-Means Clustering in R Programming