Transforming Variables with mutate()

The mutate() function from the dplyr package is the main tool for computing new variables or modifying existing ones in a data frame. It makes transformations explicit and easy to read.

We can use mutate() to create the same gender factor as we did with base R:

library(dplyr)
bfi <- mutate(bfi, gender = factor(gender, labels = c("male", "female")))

It can also perform mathematical transformations. For example, to mean-center the variable age:

bfi <- mutate(bfi, age_c = age - mean(age))
head(bfi)
      A1 A2 A3 A4 A5 C1 C2 C3 C4 C5 E1 E2 E3 E4 E5 N1 N2 N3 N4 N5 O1 O2 O3 O4
61617  2  4  3  4  4  2  3  3  4  4  3  3  3  4  4  3  4  2  2  3  3  6  3  4
61618  2  4  5  2  5  5  4  4  3  4  1  1  6  4  3  3  3  3  5  5  4  2  4  3
61620  5  4  5  4  4  4  5  4  2  5  2  4  4  4  5  4  5  4  2  3  4  2  5  5
61621  4  4  6  5  5  4  4  3  5  5  5  3  4  4  4  2  5  2  4  1  3  3  4  3
61622  2  3  3  4  5  4  4  5  3  2  2  2  5  4  5  2  3  4  4  3  3  3  4  3
61623  6  6  5  6  5  6  6  6  1  3  2  1  6  5  6  3  5  2  2  3  4  3  5  6
      O5 gender education age      age_c
61617  3   male        NA  16 -12.782143
61618  3 female        NA  18 -10.782143
61620  2 female        NA  17 -11.782143
61621  5 female        NA  17 -11.782143
61622  3   male        NA  17 -11.782143
61623  1 female         3  21  -7.782143

And to standardize age (i.e., convert it to z-scores) and turn education into a labeled factor:

bfi <- mutate(bfi,
              age_std = scale(age)[ , 1],
              education = factor(education,
                                 labels = c("some high school",
                                            "high school graduate",
                                            "some college",
                                            "college graduate",
                                            "graduate degree"))
              )
str(bfi)
'data.frame':   2800 obs. of  30 variables:
 $ A1       : int  2 2 5 4 2 6 2 4 4 2 ...
 $ A2       : int  4 4 4 4 3 6 5 3 3 5 ...
 $ A3       : int  3 5 5 6 3 5 5 1 6 6 ...
 $ A4       : int  4 2 4 5 4 6 3 5 3 6 ...
 $ A5       : int  4 5 4 5 5 5 5 1 3 5 ...
 $ C1       : int  2 5 4 4 4 6 5 3 6 6 ...
 $ C2       : int  3 4 5 4 4 6 4 2 6 5 ...
 $ C3       : int  3 4 4 3 5 6 4 4 3 6 ...
 $ C4       : int  4 3 2 5 3 1 2 2 4 2 ...
 $ C5       : int  4 4 5 5 2 3 3 4 5 1 ...
 $ E1       : int  3 1 2 5 2 2 4 3 5 2 ...
 $ E2       : int  3 1 4 3 2 1 3 6 3 2 ...
 $ E3       : int  3 6 4 4 5 6 4 4 NA 4 ...
 $ E4       : int  4 4 4 4 4 5 5 2 4 5 ...
 $ E5       : int  4 3 5 4 5 6 5 1 3 5 ...
 $ N1       : int  3 3 4 2 2 3 1 6 5 5 ...
 $ N2       : int  4 3 5 5 3 5 2 3 5 5 ...
 $ N3       : int  2 3 4 2 4 2 2 2 2 5 ...
 $ N4       : int  2 5 2 4 4 2 1 6 3 2 ...
 $ N5       : int  3 5 3 1 3 3 1 4 3 4 ...
 $ O1       : int  3 4 4 3 3 4 5 3 6 5 ...
 $ O2       : int  6 2 2 3 3 3 2 2 6 1 ...
 $ O3       : int  3 4 5 4 4 5 5 4 6 5 ...
 $ O4       : int  4 3 5 3 3 6 6 5 6 5 ...
 $ O5       : int  3 3 2 5 3 1 1 3 1 2 ...
 $ gender   : Factor w/ 2 levels "male","female": 1 2 2 2 1 2 1 1 1 2 ...
 $ education: Factor w/ 5 levels "some high school",..: NA NA NA NA NA 3 NA 2 1 NA ...
 $ age      : int  16 18 17 17 17 21 18 19 19 17 ...
 $ age_c    : num  -12.8 -10.8 -11.8 -11.8 -11.8 ...
 $ age_std  : num  -1.149 -0.969 -1.059 -1.059 -1.059 ...

These transformations make variables more interpretable and comparable across scales.

Practice

Modify the factor levels of the ‘education’ factor we just created. Replace all of the spaces with underscores, “_“.

HINT 1: The levels() function can also be used to re-assign factor levels.

HINT 2: If you want to be fancy, check out the gsub() function.

# Transform education factor levels to replace spaces with underscores
levels(bfi$education) <-  c("some_high_school","high_school_graduate", "some_college",  "college_graduate", "graduate_degree")

# Alternatively, using levels() and gsub():
levels(bfi$education) <- gsub(" ", "_", levels(bfi$education))
Back to top