二者都
在当月的观察中,中低阶级群体至少有5个人。否则,我希望它显示为NA。
library(dplyr)
#Sample dataset
test_data <- tibble(month = c(rep(c("Jan"), 4), rep(c("Feb"), 4)),
ses = c(rep(c("High", "Mid", "Mid Low", "Low"), 2)),
total = c(10, 20, 4, 30, 9, 11, 40, 60),
total_selected = c(9, 10, 8, 3, 8, 6, 8, 6))
#Failed attempt
wrong <- test_data %>%
group_by(month) %>%
mutate(adjusted_total = case_when(
ses == "Mid Low" & total[ses == "Mid"] <5 | total[ses == "Low"] <5 ~ NA_real_,
TRUE ~ total
))
使用解决方案编辑
我意识到我的代码有一个拼写错误。首先,我指的是or语句,而不是AND。其次,阈值对于我的数据来说太低了。当我调整到OR语句,并且截止到15时
correct <- tibble(month = c(rep(c("Jan"), 4), rep(c("Feb"), 4)),
ses = c(rep(c("High", "Mid", "Mid Low", "Low"), 2)),
total = c(10, 20, 4, 30, 9, 11, 40, 60),
total_selected = c(9, 10, 8, 3, 8, 6, 8, 6)) %>%
group_by(month) %>%
mutate(adjusted_total = case_when(
ses == "Mid Low" & total[ses == "Mid"] < 15 | total[ses == "Low"] < 15 ~ NA_real_,
TRUE ~ total
))