The log and log10 functions are the most useful functions in R when it comes to data analysis. The Log is the short form of the logarithm. When ever you find some analytical tasks, such as EDA and more, these functions will be invaluable for you.

The log is just like exponents. To make it simple, if the output is 25, then you will get the log value as 3. Here 3 will be the power and to put in the form of an equation, log2(25) = 5. Got confused? Worry not, we will encounter many examples as we progress.

Syntax of log and log10 functions in R

log: The log function in R helps to compute the natural log and if base is provided then it computes accordingly.

Log10: The log10 function in R computes the common log.

log(x, base=n)

Here,

X = The input value.

Base = The base to compute the value.

log10(x,base=n)

Here,

X = The input value.

Base = The base to compute the value.

Simple log and log10 functions in R

Well, if you are clear with syntax, we are good to go and compute our first log and log10 values in R programming.

log(25)

Output = 3.218876

log(64)

Output = 4.158883

log(25,base = 3)

The Output = 2.929947

This is the example which computes the log value with base 3.

That’s amazing! We have computed the log values for the input numbers. Let’s take it forward and compute the log10 values in R.

log(25,base = 2)

Output = 4.643856

log(25,base = 3)

Output = 2.929947

Good going so far. I hope now you got better at log and log10 functions in R programming. Let’s use this knowledge to compute the log values of the data present in the dataframe.

Log and log10 function with a dataframe

For this, we will first going to import a dataset in the R and then try to apply the log and log10 functions to the data present in it.

#Importing dataset in R
df <- datasets::airquality
#display data
df
     Ozone Solar.R Wind Temp Month Day
1      41     190  7.4   67     5   1
2      36     118  8.0   72     5   2
3      12     149 12.6   74     5   3
4      18     313 11.5   62     5   4
5      NA      NA 14.3   56     5   5
6      28      NA 14.9   66     5   6

well, our data is now ready. Let’s apply the log and log 10 functions to data present in particular columns.

log(df$Ozone[1:10])
3.713572 
3.583519 
2.484907 
2.890372       
NA 
3.332205 
3.135494 
2.944439
2.079442
NA

It’s great that we got the desired output. Like this you can extract any columns data and apply the log function for that.

Likewise, let’s use the log10 function for the dataframe.

log10(df$Ozone[1:10])
1.612784 
1.556303 
1.079181 
1.255273    
NA 
1.447158
1.361728
1.278754
0.903090
NA

These are the log10 values of the first 10 values.

Final words

Both log and log10 functions can be used to get the log values with base. You can use these functions on a single value, a vector, or a dataframe as well. I hope now you got better at log functions in R programming. That’s all for now. Happy R!!!.

Read: R docs