Creating Conditional Variables with case_when()
The case_when() function inside mutate() allows you to build new variables based on conditional logic, similar to “if…else” statements but more readable.
For example, we can classify participants into categories of gendered maturity based on their age and gender:
library (dplyr) # load dplyr
bfi <- mutate (bfi,
gendered_maturity = case_when (
age < 18 & gender == "male" ~ "boy" ,
age < 18 & gender == "female" ~ "girl" ,
age >= 18 & gender == "male" ~ "man" ,
age >= 18 & gender == "female" ~ "woman" ,
TRUE ~ NA_character_ ),
gendered_maturity = as.factor (gendered_maturity))
str (bfi)
'data.frame': 2800 obs. of 39 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 : int NA NA NA NA NA 3 NA 2 1 NA ...
$ age : int 16 18 17 17 17 21 18 19 19 17 ...
$ agree : num 4 4.2 3.8 4.6 4 4.6 4.6 2.6 3.6 5.4 ...
$ consc : num 2.8 4 4 3 4.4 5.6 4.4 3.4 4 5.6 ...
$ extra : num 3.8 5 4.2 3.6 4.8 5.6 4.2 2.4 3.25 4.8 ...
$ neuro : num 2.8 3.8 3.6 2.8 3.2 3 1.4 4.2 3.6 4.2 ...
$ open : num 3 4 4.8 3.2 3.6 5 5.4 4.2 5 5.2 ...
$ agree_z : num -0.726 -0.503 -0.948 -0.058 -0.726 ...
$ consc_z : num -1.541 -0.279 -0.279 -1.33 0.141 ...
$ extra_z : num -0.3253 0.8058 0.0518 -0.5138 0.6173 ...
$ neuro_z : num -0.3028 0.5331 0.3659 -0.3028 0.0315 ...
$ open_z : num -1.963 -0.726 0.264 -1.715 -1.221 ...
$ gendered_maturity: Factor w/ 4 levels "boy","girl","man",..: 1 4 2 2 1 4 3 3 3 2 ...
levels (bfi$ gendered_maturity)
[1] "boy" "girl" "man" "woman"
with (bfi, table (minor = age < 18 , gender, gendered_maturity))
, , gendered_maturity = boy
gender
minor male female
FALSE 0 0
TRUE 84 0
, , gendered_maturity = girl
gender
minor male female
FALSE 0 0
TRUE 0 164
, , gendered_maturity = man
gender
minor male female
FALSE 835 0
TRUE 0 0
, , gendered_maturity = woman
gender
minor male female
FALSE 0 1717
TRUE 0 0
Renaming Variables
Finally, the rename() and rename_with() functions from dplyr provide a clean way to rename variables, individually or in groups.
To rename a few columns directly:
bfi <- rename (bfi, gen = gender, ed = education, gm = gendered_maturity)
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 gen ed age agree consc extra neuro open agree_z consc_z
61617 3 male NA 16 4.0 2.8 3.8 2.8 3.0 -0.72583913 -1.5406915
61618 3 female NA 18 4.2 4.0 5.0 3.8 4.0 -0.50322161 -0.2793220
61620 2 female NA 17 3.8 4.0 4.2 3.6 4.8 -0.94845665 -0.2793220
61621 5 female NA 17 4.6 3.0 3.6 2.8 3.2 -0.05798656 -1.3304633
61622 3 male NA 17 4.0 4.4 4.8 3.2 3.6 -0.72583913 0.1411345
61623 1 female 3 21 4.6 5.6 5.6 3.0 5.0 -0.05798656 1.4025040
extra_z neuro_z open_z gm
61617 -0.32527289 -0.30281564 -1.9627661 boy
61618 0.80583785 0.53307316 -0.7257147 woman
61620 0.05176403 0.36589540 0.2639264 girl
61621 -0.51379134 -0.30281564 -1.7153558 girl
61622 0.61731939 0.03153988 -1.2205352 boy
61623 1.37139322 -0.13563788 0.5113367 woman
To rename several variables programmatically, use rename_with(). For instance, we can convert all item names ending in a number to lowercase:
bfi <- rename_with (bfi, .fn = tolower, .cols = matches (" \\ d$" ))
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 gen ed age agree consc extra neuro open agree_z consc_z
61617 3 male NA 16 4.0 2.8 3.8 2.8 3.0 -0.72583913 -1.5406915
61618 3 female NA 18 4.2 4.0 5.0 3.8 4.0 -0.50322161 -0.2793220
61620 2 female NA 17 3.8 4.0 4.2 3.6 4.8 -0.94845665 -0.2793220
61621 5 female NA 17 4.6 3.0 3.6 2.8 3.2 -0.05798656 -1.3304633
61622 3 male NA 17 4.0 4.4 4.8 3.2 3.6 -0.72583913 0.1411345
61623 1 female 3 21 4.6 5.6 5.6 3.0 5.0 -0.05798656 1.4025040
extra_z neuro_z open_z gm
61617 -0.32527289 -0.30281564 -1.9627661 boy
61618 0.80583785 0.53307316 -0.7257147 woman
61620 0.05176403 0.36589540 0.2639264 girl
61621 -0.51379134 -0.30281564 -1.7153558 girl
61622 0.61731939 0.03153988 -1.2205352 boy
61623 1.37139322 -0.13563788 0.5113367 woman
Please enable JavaScript to experience the dynamic code cell content on this page.
The following problem statement uses these abbreviations
O = Openness to Experience (open)
E = Extraversion (extra)
Use dplyr::mutate() and case_when() to create a new factor called type that satisfies the following logic
type = “adventurous” when O is higher than the mean of O and E is higher than the mean of E
type = “inquisitive” when O is higher than the mean of O and E is lower than or equal to the mean of E
type = “quiet” when O is lower than or equal to the mean of O and E is lower than or equal to the mean of E
type = “chatty” when O is lower than or equal to the mean of O and E is higher than the mean of E
Please enable JavaScript to experience the dynamic code cell content on this page.
bfi <- mutate (bfi,
type = case_when (
open > mean (open, na.rm = TRUE ) & extra > mean (extra, na.rm = TRUE ) ~ "adventurous" ,
open > mean (open, na.rm = TRUE ) & extra <= mean (extra, na.rm = TRUE ) ~ "inquisitive" ,
open <= mean (open, na.rm = TRUE ) & extra <= mean (extra, na.rm = TRUE ) ~ "quiet" ,
open <= mean (open, na.rm = TRUE ) & extra > mean (extra, na.rm = TRUE ) ~ "chatty" ,
TRUE ~ NA_character_ ),
type = as.factor (type))
Exclude the raw scale items from the modified ‘bfi’ data.
Save the dataset from (a) as an RDS file.
Please enable JavaScript to experience the dynamic code cell content on this page.
# a)
bfi_mod <- select (bfi, - starts_with (c ("A" , "C" , "E" , "N" , "O" )))
#b)
saveRDS (bfi_mod, file = "bfi_mod.rds" )
Back to top