Logarithm in R – How to Apply the log() Function (2 Examples)
In this R programming tutorial you’ll learn how to compute logarithms using the log function.
Construction of Exemplifying Data
x <- c(5, 1, 4, 8, 99) # Create numeric vector x # Return vector to console # 5 1 4 8 99 |
x <- c(5, 1, 4, 8, 99) # Create numeric vector x # Return vector to console # 5 1 4 8 99
Example 1: Basic Application of log() Function in R
log(x) # Using log function # 1.609438 0.000000 1.386294 2.079442 4.595120 |
log(x) # Using log function # 1.609438 0.000000 1.386294 2.079442 4.595120
Example 2: Change Base of log Function
log(x, base = 2) # Specifying base of 2 # 2.321928 0.000000 2.000000 3.000000 6.629357 |
log(x, base = 2) # Specifying base of 2 # 2.321928 0.000000 2.000000 3.000000 6.629357
log2(x) # Using log2 function (same result) # 2.321928 0.000000 2.000000 3.000000 6.629357 |
log2(x) # Using log2 function (same result) # 2.321928 0.000000 2.000000 3.000000 6.629357
log(x, base = 10) # Specifying base of 10 # 0.698970 0.000000 0.602060 0.903090 1.995635 |
log(x, base = 10) # Specifying base of 10 # 0.698970 0.000000 0.602060 0.903090 1.995635
log10(x) # Using log10 function (same result) # 0.698970 0.000000 0.602060 0.903090 1.995635 |
log10(x) # Using log10 function (same result) # 0.698970 0.000000 0.602060 0.903090 1.995635