Make sure you have spaces placed correctly so that mfaz < -3 does not get interpreted as mfax <- 3. Notice the difference between the first and second uses of case_when in the code below.
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
df_childanthro <- data.frame(mfaz = c(-1, -2.1, -3.1 , -1),
age_months_correct = c(60, 60, 60, 50))
df_childanthro <- df_childanthro %>%
mutate(mfa_category = case_when(mfaz >= -2 & age_months_correct >= 59 ~ "MUAC >=-2",
(mfaz < -2 & mfaz >= -3) & age_months_correct >= 59 ~ "MUAC <-2 & >=-3",
mfaz < -3 & age_months_correct >= 59 ~ "MUAC <-3",
age_months_correct < 59 ~ "N/A- Less than 5 years old",
TRUE ~ "missing"))
df_childanthro
#> mfaz age_months_correct mfa_category
#> 1 -1.0 60 MUAC >=-2
#> 2 -2.1 60 MUAC <-2 & >=-3
#> 3 -3.1 60 MUAC <-3
#> 4 -1.0 50 N/A- Less than 5 years old
df_childanthro <- df_childanthro %>%
mutate(mfa_category = case_when(mfaz >= -2 & age_months_correct >= 59 ~ "MUAC >=-2",
(mfaz < -2 & mfaz >= -3) & age_months_correct >= 59 ~ "MUAC <-2 & >=-3",
mfaz <-3 & age_months_correct >= 59 ~ "MUAC <-3",
age_months_correct < 59 ~ "N/A- Less than 5 years old",
TRUE ~ "missing"))
df_childanthro
#> mfaz age_months_correct mfa_category
#> 1 -1.0 60 MUAC >=-2
#> 2 -2.1 60 MUAC >=-2
#> 3 -3.1 60 MUAC >=-2
#> 4 -1.0 50 N/A- Less than 5 years old