An alternative to case_when
would be to try if_else
. You can see an example of this with one of the built in data sets. For example, chickwts. The variable feed is a categorical variable. If, in our example, the target is to create a new dummy variable based on the value of feed, then if_else serves our purpose. However, case_when
is a better choice when there are more conditions. I've included two extra commands (i.e., glimpse
and table
) for you to test your work.
# package library
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# load sample data
head(force(chickwts))
#> weight feed
#> 1 179 horsebean
#> 2 160 horsebean
#> 3 136 horsebean
#> 4 227 horsebean
#> 5 217 horsebean
#> 6 168 horsebean
# inspect the sample data
# there are two variables, feed is a categorical variable
glimpse(chickwts)
#> Rows: 71
#> Columns: 2
#> $ weight <dbl> 179, 160, 136, 227, 217, 168, 108, 124, 143, 140, 309, 229, 181…
#> $ feed <fct> horsebean, horsebean, horsebean, horsebean, horsebean, horsebea…
# record a categorical value to a new categorical variable
dat_updated <- chickwts %>%
mutate(
# if the feed is casein then d_casein is TRUE
d_casein = if_else(
condition = feed == "casein",
true = TRUE,
false = FALSE
)
)
# inspect with a contingency table
table(dat_updated$feed, dat_updated$d_casein)
#>
#> FALSE TRUE
#> casein 0 12
#> horsebean 10 0
#> linseed 12 0
#> meatmeal 11 0
#> soybean 14 0
#> sunflower 12 0
Created on 2023-08-23 with reprex v2.0.2