The log() function computes logarithms, by default, it calculates the natural logarithms. The log10() function computes common (for example, base 10) logarithms. The log2() function computes binary (i.e., base 2) logarithms. The common log(x, base) function computes logarithms with base.
log2 in R
The log2() is an inbuilt R mathematical function that calculates the binary (base 2) logarithm. The log() function calculates natural logarithms by default. The log2() method computes the logarithm of the given column in base 2.
It takes x as a column to calculate.
Let’s find the log2 of 11 using the log2() function.
Let’s find the log2 value of 0 and 1.
 0  -Inf
Plot the log2() function in R
The most used plotting function in R programming is the plot() function. It is an inbuilt generic function that has many methods called according to the type of object passed to the plot().
Open the rstudio and write the following code.
data <- seq(0, 1, by = 0.1) plot(data, log2(data), typ = "l", col = "red")
Find the log2() of vector
To find the log2 of vector, pass the vector to the log2() function. Before that, you have to create a vector sequence if it is not available to you. Let’s create a vector sequence for our example.
vc <- (rep(1:5)) cat("The vector sequence is: ") cat(vc, "\n")
The vector sequence is: 1 2 3 4 5
To find the log2 of this vector, pass the sequence to a log2() function.
vc <- (rep(1:5)) cat("The vector sequence is: ") cat(vc, "\n") cat("Find the log2() of vector sequence is: ") cat(log2(vc))
The vector sequence is: 1 2 3 4 5 Find the log2() of vector sequence is: 0 1 1.584963 2 2.321928
That is it for log2() function in R language.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.