log Function in R (5 Examples) | Natural, Binary & Common Logarithm


This post shows how to compute logarithms using the log function in the R programming language.

Table of contents:

You’re here for the answer, so let’s get straight to the examples…

Definition & Basic R Syntax of log Function


Definition: The log R function computes logarithms of numeric values.


Basic R Syntax: You can find the basic R programming syntax of the log function below.

log(x)                       # Basic R syntax of log function


In the following, I’ll show five examples for the application of the log function in the R programming language. So keep on reading…

Example 1: Apply log Function to Numeric Value

This Example explains how to apply the log function to a single numeric value. For this, we simply have to insert the value, for which we want to calculate the logarithm, into the log() function:

log(3)                       # Applying log function
# 1.098612

The RStudio console returns the result: The logarithm of 3 is 1.098612


Example 2: Apply log Function with User-Defined Base

Example 1 explained how to compute the natural logarithm (default specification of the log function). In Example 2, I’ll show how to change the base of the log command. For this task, we need to specify the base argument of the log function as shown below:

log(3, base = 5)             # Base of 5
# 0.6826062

You can see the logarithm of 3 with a base of 5 in the output above.


Example 3: The log2 Function

The R programming language provides some wrapper functions for common types of logarithms. In this Example, I’ll explain how to use the log2 function to calculate a logarithm with a base of 2 (i.e. binary logarithm). First, consider the R programming syntax we have already used in the previous example:

log(3, base = 2)             # Base of 2
# 1.584963

As you can see, the logarithm of 3 with a base of 2 is 1.584963.

However, we can also use the log2 function to obtain the same result with a more efficient R syntax:

log2(3)                      # Applying log2 function
# 1.584963


Example 4: The log10 Function

In this Example, I’ll show how to compute the common logarithm (i.e. a base of 10) using the log10 function. First, let’s compute the common logarithm with the conventional R syntax:

log(3, base = 10)            # Base of 10
# 0.4771213

Similar to the previous example, the R programming language provides a wrapper function for the common logarithm:

log10(3)                     # Applying log10 function
# 0.4771213


Example 5: Apply log Function to Vector

This Example illustrates how to apply the log function to a vector object. First, let’s create an example vector in R:

my_vec <- 1:10               # Create example vector
my_vec                       # Print vector to RStudio console
# 1  2  3  4  5  6  7  8  9 10

Our example vector contains ten numeric values ranging from 1 to 10.

Now, we can apply the log function to this vector as shown below:

log(my_vec)                  # Apply log function to vector
# 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379 1.7917595 1.9459101 2.0794415 2.1972246 2.3025851

We could also change to base of the log function when it’s applied to a vector:

log(my_vec, base = 5)        # Base of 5
# 0.0000000 0.4306766 0.6826062 0.8613531 1.0000000 1.1132828 1.2090620 1.2920297 1.3652124 1.4306766


Video, Further Resources & Summary

If you need more explanations on the R syntax of this page, I can recommend to watch the following video of my YouTube channel. In the video, I show the R code of this article.


The YouTube video will be added soon.


Furthermore, you may want to read some of the related articles of my website. I have released several related tutorials already:


At this point you should know how to apply the log function in the R programming language. Tell me about it in the comments below, in case you have additional comments and/or questions.


Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.