How to calculate the log() of a value in R
The log() function in R calculates the logarithm of a number to the base. log() calculates the natural logarithm of a number if the base is not specified by the user.
The following illustration shows the mathematical representation of the log() function.
Syntax
log(number, base)
Parameters
This function requires two parameters:
- A
for which the logarithm is to be calculated.number must be greater than 0 - The
baseof the is an optional parameter.logarithm a number that must be greater than one
Return value
log() returns the logarithm of a number to the base sent as a parameter.
Remember that the
log()function calculates the natural logarithm of a number if the base is not provided, i.e.,log(y,e).
If either of the parameters (i.e. base and number) is a negative number or zero, then
log()returnsNaNwith awarningerror orinfinity.
Code
#log without basea <- log(20);print(paste0("log(20): ", a))#log with baseb <- log(2,2);print(paste0("log(2,2): ", b))#error outputsc <- log(0);print(paste0("log(0): ", c))d <- log(-1);print(paste0("log(-1): ", d))