基于因子R的级别的条件过滤

时间:2014-07-10 15:01:41

标签: r dplyr

我想清理以下代码。具体来说,我想知道我是否可以合并三个过滤语句,以便最终得到最终的data.frame(rind()),其中包含数据行“spring”(如果存在),数据行为“如果“春天”不存在,最后如果既不存在“春天”也不存在“秋天”,那么数据行就会存在。下面的代码看起来非常笨重和低效。我试图让自己为(),所以希望解决方案不会涉及一个。可以使用dplyr完成吗?

# define a %not% to be the opposite of %in%
library(dplyr)
`%not%` <- Negate(`%in%`)
f <- c("a","a","a","b","b","c")
s <- c("fall","spring","other", "fall", "other", "other")
v <- c(3,5,1,4,5,2)
(dat0 <- data.frame(f, s, v))
sp.tmp <- filter(dat0, s == "spring")
fl.tmp <- filter(dat0, f %not% sp.tmp$f, s == "fall")
ot.tmp <- filter(dat0, f %not% sp.tmp$f, f %not% fl.tmp$f, s == "other")
rbind(sp.tmp,fl.tmp,ot.tmp)

1 个答案:

答案 0 :(得分:3)

在每个f组中,您希望按照首选项的降序提取springfallother。< / p>

如果您首先按优先顺序排列实际因素排序:

dat0$s <- factor(dat0$s, levels=c("spring", "fall", "other"))

然后您可以使用this dplyr solution获取每个组中的最小行(相对于该因子):

newdat <- dat0 %.% group_by(f) %.% filter(rank(s) == 1)
相关问题