Related Tags

r
math
logarithm
communitycreator

# How to calculate the log() of a value in R

harris910

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.

Mathematical representation of the log() function

### Syntax

log(number, base)


### Parameters

This function requires two parameters:

• A numbermust be greater than 0 for which the logarithm is to be calculated.
• The base of the logarithma number that must be greater than one is an optional parameter.

### 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() returns NaN with a warning error or infinity.

### Code

#log without base
a <- log(20);
print(paste0("log(20): ", a))

#log with base
b <- log(2,2);
print(paste0("log(2,2): ", b))

#error outputs
c <- log(0);
print(paste0("log(0): ", c))

d <- log(-1);
print(paste0("log(-1): ", d))

RELATED TAGS

r
math
logarithm
communitycreator

CONTRIBUTOR

harris910
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time 