基于因子R的级别进行条件过滤 [英] Conditional filtering based on the level of a factor R

查看:81
本文介绍了基于因子R的级别进行条件过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想清理以下代码。具体来说,我想知道是否可以整合三个过滤器语句,以便最终得到包含数据行spring的最终的data.frame(rind()),如果存在,那么如果弹簧不存在,则最后一行数据不存在弹簧或跌落。下面的代码看起来很笨重,效率低下。我试图释放自己的(),所以希望解决方案不会涉及到一个。这可以使用dplyr吗?

I would like to clean up the following code. Specifically, I'm wondering if I can consolidate the three filter statements so that I end up with the final data.frame (the rind()) that contains the row of data "spring" if it exists, the row of data for "fall" if "spring" doesn't exist, and finally the row of data if neither "spring" nor "fall" exist. The code below seems very clunky and inefficient. I am trying to free myself of for(), so hopefully the solution won't involve one. Could this be done using 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)


推荐答案

看起来像每组 f ,您要按照优先级顺序从 spring fall 其他

It looks like within each group of f, you want to extract the row of, in descending order of preference, spring, fall, or other.

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

If you first make your ordering of preference the actual factor ordering:

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

然后,您可以使用此dplyr解决方案获取最小行(相对于该因子):

Then you can use this dplyr solution to get the minimum row (relative to that factor) within each group:

newdat <- dat0 %.% group_by(f) %.% filter(rank(s) == 1)

这篇关于基于因子R的级别进行条件过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆