0

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. Thus I am trying to filter out all the observations without joint problem (jointprob "No" and hlthcare "Yes" and "No"). So observations with jointprob "Yes" should be taken out.

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
user5249203
  • 4,436
  • 1
  • 19
  • 45
Seo Jung Park
  • 23
  • 1
  • 4
  • `&&` is for a single condition. You want the vectorized version `&`. – Gregor Thomas Jan 25 '18 at 19:01
  • `&&` returns one boolean value from a vector of comparisons, `&` returns a vector of boolean values from a vector of comparisons (which is what you're looking for). See if this helps answer helps: [https://stackoverflow.com/questions/6558921/boolean-operators-and](https://stackoverflow.com/questions/6558921/boolean-operators-and). – twedl Jan 25 '18 at 19:01
  • It worked perfectly!! Thanks so much! – Seo Jung Park Jan 25 '18 at 19:13

0 Answers0