jeudi 25 janvier 2018

R - multiple logical commands (combining variables to create a new variable)

I am trying to create a variable by combining two variables. However, I don't know why my command does not give me the expected result.

For the dataset, I wil use the name 'dataset.' I am trying to make a variable PopQ1 by combining jointprob variable and hlthcare variable. These variables have 3 outputs which are "Yes" "No" and NA. I want those people who do not have joint problems to be my target population; the population (without joint problems) with or without healthcare is the target population which I later want to create boxplot with.

dataset <- dataset %>%
  mutate(PopQ1 = ifelse(jointprob == "No" && hlthcare == "Yes", "Yes", ifelse(jointprob == "No" && hlthcare == "No", "No", NA)))

However, although it does not give me any error when I run the command, but if I create a table in order to compare, it gives all NA in the PopQ1 variable. Could you please help me what went wrong in this?

brfss2013 %>%
  group_by(jointprob, hlthcare, PopQ1) %>%
  summarise(count = n())

# A tibble: 9 x 4
# Groups: jointprob, hlthcare [?]
#   jointprob hlthcare PopQ1  count
#   <fct>    <fct>    <lgl>  <int>
# 1 Yes      Yes      NA     69850
# 2 Yes      No       NA      6258
# 3 Yes      NA       NA       187
# 4 No       Yes      NA     72391
# 5 No       No       NA      4539
# 6 No       NA       NA       155
# 7 NA       Yes      NA    292330
# 8 NA       No       NA     44503
# 9 NA       NA       NA      1562

Aucun commentaire:

Enregistrer un commentaire