The log() and log10() functions in R are the most useful functions in Data analysis, in particular, Exploratory Data Analysis.
Log is short for Logarithm. In simple words, a Logarithm is just like exponents.
For example, if the final value is 16, what is the power you will add to 2?
If your answer is 4, then take a moment and appreciate yourself, you are good at maths!
If you have to express this in terms of the log, then you should write it as -log2(16) = 4. Got an idea about working on a logarithm? If yes, then we are good to go!
Table of Contents
Syntax of log() function in R
log() and log10(): The log function computes the natural log (Ln) and if the base is given, it will compute with base. On the other hand, the log10() function computes the common log (Lg).
X = Input value.
Base = If mentioned, log value will be computed with base.
A simple log() function in R
The log() function in R will take the value as an input and returns the logarithmic value of the same as output. In this section, let’s pass a simple number to the log function to know its working.
#Computes the logarithmic value of an input number log(2)
As you can see in the above output, the log function takes the input value and computes its log value.
#Computes the logarithmic values of a decimal value log(5.2)
You can pass all the real numbers along with decimal numbers. You can mention the base value to be computed as well. Let’s look at an example to make it simple.
#Computes log value with base log(16,base=2)
Note: You cannot pass a Negative number to the log function. If you do so, it will return a NaN error.
A simple log10() function in R
Like the log() function, the log10() function also takes numerical input and computes the common log value for the same. Let’s see how it works.
#Computes the common log for the input value log10(5)
As you can see the above output shows the common log for the input value.
#Computes the common log for the input values (decimal) log10(123.567)
Again the same rule — Do not use negative values as input.
Log() and log10() function with a vector
Let’s create a vector that includes multiple numerical values and let’s pass that vector as input to the log() function to get the log values.
#Creates a vector with values df<-c(23,45,6.7,55,88.9,4,3.789) #Computes the log value log(df)
3.135494 3.806662 1.902108 4.007333 4.487512 1.386294 1.332102
Well, the log() function returned all the log value of the input. Now, let’s try this with the base value as well.
#Creates a vector with values df<-c(23,45,6.7,55,88.9,4,3.789) #Computes the log value log(df,base=2)
4.523562 5.491853 2.744161 5.781360 6.474112 2.000000 1.921817
Wow!! R is superfast and returned all the log values with the base value 2. I hope you are getting better with these functions in R. You can do the same using log10() function as well.
#Creates a vector with values df<-c(23,45,6.7,55,88.9,4,3.789) #Computes the log value log10(df)
1.3617278 1.6532125 0.8260748 1.7403627 1.9489018 0.6020600 0.5785246
That’s it! You did it effortlessly.
log() and log10() functions with a dataframe
Till now – Too good. Let’s take this little forward by using the log() and log10() functions to compute the log values of a dataframe.
Let’s import a dataset and compute the log values for the entire values in a specific columns.
#Importing the dataset datasets::airquality
This is a “Airquality” dataset and let’s apply log() function to the values present in the “Temp” column.
4.204693 4.276666 4.304065 4.127134 4.025352 4.189655 4.174387 4.077537 4.1108744.234107
Fantastic!!! You got the top 10 values in the “Temp” column and the log() function returned the log values of the same. Let’s do the same using the log10() function in R.
1.826075 1.857332 1.869232 1.792392 1.748188 1.819544 1.812913 1.770852 1.785330 1.838849
Like this, you can make use of log() and log10() function in R to get the log values of the input.
Plotting the log value
You did almost everything using the log() and log10() functions in R. Now it’s time to plot the results.
Are you excited??
#Plots the graph using the log values new_plot<-seq(0,100,by=0.1) plot(new_plot,log(x),type = 'l',col='Red')
That’s it. It’s very easy to plot the results of a log values as shown above.
The log() and log10() functions are beautiful functions in the R language.
When it comes to data analysis, these functions are invaluable. It will take the input values and computes the natural and common log respectively.
This article clearly explains the application of these functions in a simple but effective way. That’s all for now. Happy Logging!!!
More read: R documentation.