0

So I'm doing my research for the final assignment at Coursera and the thing is that the nested ifelse statement doesn't work as expected.

Below is the code I've implemented.

topic <- topic %>%
  filter(!is.na(fear), !is.na(owngun), !is.na(partyid))

topic <- topic %>%
  mutate(fear = as.numeric(fear)) %>%
  mutate(owngun = as.numeric(owngun))

topic %>%
  group_by(fear, owngun) %>%
  summarise(count = n())

topic <- topic %>%
  mutate(fear_gun = ifelse(fear == 1 && owngun == 1, 1, ifelse(fear == 1 && owngun == 2, 2, ifelse(fear == 2 && owngun == 1, 3, ifelse(fear == 2 && owngun == 2, 3, -1)))))

topic %>%
  group_by(fear_gun) %>%
  summarise(count = n())

And here is the result. R markdown

So it seems like the result is not correctly sorted. What's the problem here?

zx8754
  • 52,746
  • 12
  • 114
  • 209
user3551261
  • 53
  • 1
  • 6

0 Answers0