【发布时间】:2023-03-03 14:19:02
【问题描述】:
使用下面提供的示例数据:对于每种机构类型(“a”和“b”),如果存在 fac ==“yes”的行,我想删除带有 fac ==“no”的行年。然后我想按年份对这些值求和。但是,我无法弄清楚如何删除正确的“否”行。以下是我根据here 的答案所做的一些尝试。
set.seed(123)
ext <- tibble(
institution = c(rep("a", 7), rep("b", 7)),
year = rep(c("2005", "2005", "2006", "2007", "2008", "2009", "2009"), 2),
fac = rep(c("yes", "no", "no", "no", "no", "yes", "no"), 2),
value = sample(1:100, 14, replace=T)
)
ext %>%
group_by(institution, year) %>%
filter(if (fac == "yes") fac != "no")
ext %>%
group_by(institution, year) %>%
case_when(fac == "yes" ~ filter(., fac != "no"))
ext %>%
group_by(institution, year) %>%
{if (fac == "yes") filter(., fac != "no")}
【问题讨论】: