嵌套的ifelse语句在R中不起作用

时间:2017-06-08 09:56:50

标签: r dplyr

所以我正在研究Coursera的最终作业,问题是嵌套的ifelse语句不能按预期工作。

以下是我实施的代码。

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())

这是结果。 R markdown

所以看起来结果没有正确排序。这有什么问题?

0 个答案:

没有答案
相关问题