Apply Log Transformation to All Columns of Data Frame in R (Example Code)

This tutorial demonstrates how to apply a log transformation to all columns of a data frame in R.

Creation of Example Data

data(iris)                       # Load and manipulat example data
iris_num <- iris[ , 1:4]
head(iris_num)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width
# 1          5.1         3.5          1.4         0.2
# 2          4.9         3.0          1.4         0.2
# 3          4.7         3.2          1.3         0.2
# 4          4.6         3.1          1.5         0.2
# 5          5.0         3.6          1.4         0.2
# 6          5.4         3.9          1.7         0.4

Example: Log Transformation of All Cells in Data Frame

iris_num_log <- log(iris_num)    # Apply log() function
head(iris_num_log)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width
# 1     1.629241    1.252763    0.3364722  -1.6094379
# 2     1.589235    1.098612    0.3364722  -1.6094379
# 3     1.547563    1.163151    0.2623643  -1.6094379
# 4     1.526056    1.131402    0.4054651  -1.6094379
# 5     1.609438    1.280934    0.3364722  -1.6094379
# 6     1.686399    1.360977    0.5306283  -0.9162907

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top