You can use the **log()** function in R to calculate the log of some value with a specified base:

#calculate log of 9 with base 3 log(9, base=3)

If you don’t specify a base, R will use the default base value of *e*.

#calculate log of 9 with baseelog(9) [1] 2.197225

The following examples show how to use this function in practice.

**Example 1: Calculate Log of Single Value**

The following code shows how to calculate the log of individual values in R using different bases:

#calculate log of 100 with baseelog(100) [1] 4.60517 #calculate log of 100 with base 10 log(100, base=10) [1] 2 #calculate log of 100 with base 3 log(100, base=3) [1] 4.191807

**Example 2: Calculate Log of Values in Vector**

The following code shows how to calculate the log of every value in a vector in R:

#define vector x <- c(3, 6, 12, 16, 28, 45) #calculate log of each value in vector with baseelog(x) [1] 1.098612 1.791759 2.484907 2.772589 3.332205 3.806662

**Example 3: Calculate Log of Values in Data Frame**

The following code shows how to calculate the log of values in a specific column of a data frame in R:

#define data frame df <- data.frame(var1=c(1, 3, 3, 4, 5), var2=c(7, 7, 8, 3, 2), var3=c(3, 3, 6, 6, 8), var4=c(1, 1, 2, 8, 9)) #calculate log of each value in 'var1' column log(df$var1, base=10) [1] 0.0000000 0.4771213 0.4771213 0.6020600 0.6989700

And the following code shows how to use the **sapply()** function calculate the log of values in every column of a data frame:

#define data frame df <- data.frame(var1=c(1, 3, 3, 4, 5), var2=c(7, 7, 8, 3, 2), var3=c(3, 3, 6, 6, 8), var4=c(1, 1, 2, 8, 9)) #calculate log of values in every column sapply(df, function(x) log(x, base=10)) var1 var2 var3 var4 [1,] 0.0000000 0.8450980 0.4771213 0.0000000 [2,] 0.4771213 0.8450980 0.4771213 0.0000000 [3,] 0.4771213 0.9030900 0.7781513 0.3010300 [4,] 0.6020600 0.4771213 0.7781513 0.9030900 [5,] 0.6989700 0.3010300 0.9030900 0.9542425

**Additional Resources**

How to Transform Data in R (Log, Square Root, Cube Root)

How to Use the Square Root Function in R

How to Find the Antilog of Values in R