Natural Log in R

The natural logarithm is a logarithmic function that is the inverse of the exponential function. It is the logarithm of a number to the base e. The natural log is often used to take the antilog of a number. It can model simple growth patterns such as population growth or compound interest.

There are two ways to find the natural log in R. This guide shows you how to use both methods.

Method #1: Calculating Natural Log using log() function

log() function comes preloaded in R. It allows the users to input the base of the log. But, if you don’t mention the base, the log() function will, by default, calculate the natural logarithm.

Syntax of log() Function

log(x, base)

Arguments

  • x = a numeric or complex vector
  • base = base of the log

Example 1: Calculating Natural Log using the log() function

#calculating natural log of 5
log(5)

Output

[1] 1.609438

Example 2: Calculating Natural Log of a vector using the log() function

#creating a vector
x <- c(1,2,3,4,5)
#calculating natural log of 5
log(x)

Output

[1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379

In this example, the log() function calculates each element’s natural log inside the vector.

Method 2: Calculating Natural Log using ln() function

Installing SciViews

ln() comes from the SciViews package. To install and load the package, run the following code:

install.packages('SciViews')
library('SciViews')

Syntax of ln() Function

ln(x)

Arguments

  • x = a numeric or complex vector.

Example 1: Calculating Natural Log using the ln() Function

#loading the SciViews package
library('SciViews')
#calculating natural log of 5
ln(5)

Output

[1] 1.609438

Example 2: Calculating Natural Log of a Vector using the log() Function

#loading the SciViews package
library('SciViews')
#creating a vector
x <- c(1,2,3,4,5)
#calculating natural log of 5
ln(x)

Output

[1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379

In this example, the ln() function calculates each element’s natural log inside the vector.

Leave a Comment