简体   繁体   English

有没有使用dplyr过滤器从数据帧中删除行的更有效的方法?

[英]Is there a more efficient way of using dplyr filter to remove rows from a dataframe?

I have a large dataframe from which I wish to remove some subjects (all subjects in procedure 2 where subject ID is "4") 我有一个很大的数据框,希望从中删除一些主题(过程2中的所有主题,其中主题ID为“ 4”)

Example (and cut down) dataset is here: http://pastebin.com/raw/Dz6xxgM3 示例(和缩减的)数据集在这里: http : //pastebin.com/raw/Dz6xxgM3

My dplyr filter line is 我的dplyr过滤器行是

library(dplyr)
df<-read.table("http://pastebin.com/raw/Dz6xxgM3")
  filter(df,
    proc == "1" | proc == "3" | proc== "4" | proc =="5"  | (proc=="2" & subject != "4") 
  )

This works but seems cludgy - I have to put a regex in to include all of the procedures as well as proc ==2. 这有效,但似乎很笨拙-我必须放置一个正则表达式以包括所有过程以及proc == 2。

Is there a more elegant/efficient way to delete the rows for subject 4 in procedure 2 ? 是否有一种更优雅/有效的方法来删除过程2中主题4的行?

Cheers Pete 干杯皮特

We can use %in% instead of == to check for multiple values in the 'proc' column. 我们可以使用%in%代替==来检查“ proc”列中的多个值。

 df %>% 
     filter(proc %in% c(1,3:5)|(proc==2 & subject !=4))

You could probably condense to a not expression like 您可能会浓缩成一个not表达式,例如

filter(!(subject=='4' & proc=='2'))

as an alternative. 作为备选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM