R Markdown

How do I…log transform a column in a dataframe?

Log transformation in R is accomplished by applying the log() function to vector, data-frame or other data set. Before the logarithm is applied, 1 is added to the base value to prevent applying a logarithm to a 0 value.

vec1 <- c(100,40,44,21,32,8)
vec2<- c(6, 9, 45, 22, 88, 102)
df1 <- data.frame(vec2,vec1)
log1 <-log(vec1 + 1)
log2 <-log(vec2  + 1)
log(df1)
##       vec2     vec1
## 1 1.791759 4.605170
## 2 2.197225 3.688879
## 3 3.806662 3.784190
## 4 3.091042 3.044522
## 5 4.477337 3.465736
## 6 4.624973 2.079442

Resources / references

https://www.programmingr.com/tutorial/natural-log-in-r/

Keywords