How to calculate the log2() of a value in R

The log2() function in RA programming language based on statistical computing and graphics returns the logarithm of a number to the base 2.

Figure 1 below shows the mathematical representation of the log2() function.

Figure 1: Mathematical representation of the log2() function

Syntax


log2(number);

Parameter

This function requires a number whose logarithm base 2 must be calculated at greater than zero.

  • If the number is zero, then this function outputs -Inf.

  • If the number is negative, then this function outputs NaN along with a warning message.

Return value

log2() returns the logarithm of a number to the base 2.

Code

a <- log2(10);
print(paste0("log2(10): ", a))
b <- log2(2);
print(paste0("log2(2): ", b))
c <- log2(15.2);
print(paste0("log2(15.2): ", c))
#error outputs
d <- log2(0);
print(paste0("log2(0): ", d))
e <- log2(-1);
print(paste0("log2(-1): ", e))