Here's a dplyr::case_when
that should work. You were close, but simply need to get the syntax correct. This will create a new column with these weight_class
values applied based on the BMI
. The final TRUE
argument means if anything didn't fall into a group it will receive the value of unknown
.
df <- data %>%
mutate(weight_class = case_when(
BMI < 18.5 ~ 'underweight',
between(BMI, 18.5, 24.9) ~ 'normal',
between(BMI, 25, 29.9) ~ 'overweight',
between(BMI, 30, 34.9) ~ 'obese',
BMI > 35 ~ 'extreme',
TRUE ~ 'unknown'
))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…