所以我正在研究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())
所以看起来结果没有正确排序。这有什么问题?