In this tutorial, we will look at how to compute the logarithm of a value using log functions in R with the help of some examples.
How to compute the log of a value in R?
You can use the built-in log()
function to compute the log of a value in R. Pass the value for which you want to compute the log as an argument. The following is the syntax –
# log function in R log(x, base)
The log()
function takes the following arguments –
x
– The value for which you want to compute the log.base
(optional) – The logarithmic base to use.
By default, the log()
function computes the natural log of a value if you don’t specify the base. If you specify the base, it computes the log with respect to the given base.
Note that R also has direct functions to compute the logarithm with common bases such as log10()
to compute log with 10 as the base and log2()
to compute log with 2 as the base.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
Examples
Let’s look at some examples of using the above functions in R.
Natural log in R
To get the natural log of a value in R, pass the value to the log()
function without any additional arguments. For example, let’s compute the natural log of some numbers using this method –
# natural log of value print(log(1)) print(log(2))
Output:
[1] 0 [1] 0.6931472
We get the natural log of 1 as 0 and 2 as 0.693.
Log with base 10 in R
Let’s now compute the log with base 10 for a value. For this, you can either directly use the log10()
function or use the log()
function with base=10
.
Let’s look at an example.
# log with base 10 print(log10(100)) print(log(100, 10))
Output:
[1] 2 [1] 2
Here, we compute the log with base 10 of the number 100 using the functions log10()
and log()
(with the base as 10). You can see that we get the same result from both methods.
Log with base 2 in R
You can similarly compute the log with base 2 for a value in R. That is, either directly use the log2()
function or use the log()
function with base=2
.
Let’s look at an example.
# log with base 2 print(log2(8)) print(log(8, 2))
Output:
[1] 3 [1] 3
Here, we compute the log with base 2 of the number 8 using the functions log2()
and log()
(with the base as 2). You can see that we get the same result from both methods.
Log with custom base for a value in R
As shown above, you can use the log()
function with a custom base value. For example, let’s compute the log of 9 with base 3.
# log with custom base print(log(9, 3))
Output:
[1] 2
We get 2 as the output.
In this tutorial, we looked at how to compute the logarithm of a value in R using the function log()
which computes the natural log by default but can be customized to compute the log with respect to a different base using the base
parameter. We also looked at some specific log functions such as log10()
and log2()
which compute the log with respect to base 10 and 2 respectively.
You might also be interested in –
- Common Trigonometric Functions in R
- Get the Square Root in R using sqrt()
- Get the Absolute Value in R using abs()
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.