R Advanced

Calculating Natural Log using log() Function in R

Overview

The easiest way to calculate the natural log of a number or vector in R is to use the built-in log() function.

The natural log uses “e” as its base, whereas log10 uses 10. The “e” is Euler’s number, whose value is approximately 2.718.

In R, the formula for the natural log is ln(x) = loge(x), where e is the base.

One common confusion is that if you follow the “Calculators (mechanical device),” natural log means log10. However, if you are working with the language R, log is the natural log, and log10 is a different logarithm.

In this article, we will calculate the log of a value to the base e. Our core focus is on the natural log and not different bases.

Visual Representation

Natural logarithm of numeric value

Let’s take e as base and num as 100 and find the logarithm.

log(100) # 4.60517

You can see that the natural log of 100 is 4.60517.

Natural logarithm of numeric vector

Define a numerical vector, pass the vector to the log() function, and see the output.

numbers_vec <- c(0, 1, 2, 10, 100)

natural_log_numbers <- log(numbers_vec)

natural_log_numbers 

# [1] -Inf 0.0000000 0.6931472 2.3025851 4.6051702

You can see that the output is also a vector but each element is the natural log of input vector’s each element.

The natural log of 0 is -Inf.

The natural log of 100 is 4.60517, as we already saw earlier.

Complex numbers

Complex numbers are built upon real and imaginary numbers, but R handles them with ease because it has built-in capabilities to handle complex values well.

Define a vector of complex numbers and then calculate its ln values.

complex_vec <- c(0 + 1i, 1 + 3i, 19 + 21i)

natural_log_complex <- log(complex_vec)

natural_log_complex

# 0.000000+1.570796i 1.151293+1.249046i 3.343554+0.835357i

Plotting the log() function

To plot the log() function on the graph, we can use the “curve()” function, which accepts the log() function and various other arguments and is used in conjunction with the abline() function to draw the horizontal axis.

# Plotting the natural logarithm function

curve(log(x),
  from = 0.1, to = 10, col = "blue", lwd = 2,
  xlab = "x", ylab = "ln(x)",
  main = "Graph of Natural Logarithm (ln(x))"
)

abline(h = 0, col = "red", lty = 2) # Horizontal axis
abline(v = 1, col = "green", lty = 2) # ln(1) = 0

That’s all!

Recent Posts

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

3 days ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

2 weeks ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

3 weeks ago

Printing an Output of a Program in R

When working with R in an interactive mode, you don't need to use any functions…

4 weeks ago

How to Calculate Variance in R

To calculate the sample variance (measurement of spreading) in R, you should use the built-in…

1 month ago

tryCatch() Function in R

The tryCatch() function acts as a mechanism for handling errors and other conditions (like warnings…

1 month ago