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!

Share
Published by
Krunal Lathiya

Recent Posts

What is is.factor() Function in R

R consists of various data types, and "factor" is one of them. You can use…

19 hours ago

Efficiently Check If a Vector is Empty in R

The most efficient and idiomatic way to check if a vector is empty in R…

2 days ago

Checking If a Data Frame is Empty in R

What criteria are being evaluated to determine if a data frame is empty? There is…

3 days ago

How to Check If a List is Empty in R

What do we mean when we say an empty list? An empty list does not…

4 days ago

is.element() Function: Check Presence of Elements in R

Whether you want to do membership testing, filter data, identify missing values, check for duplicates,…

5 days ago

as.double() and is.double() Functions in R Language

Whether you want to perform calculations efficiently or derive accurate analysis, you need a double-precision…

1 week ago