Creating anthropometric categories

Hi there- trying to creating categories of a numeric variable :

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")) #all other cases

However, children with a mfaz -3.1 are still showing as MUAC >=-2

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

Created on 2021-03-09 by the reprex package (v0.3.0)

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.