简体   繁体   English

根据r中多列中其他行的条件值删除行

[英]Remove a row based on conditional value of other rows in multiple columns in r

I am trying to remove a row on the condition that it does not have a specific value in another row based on the same column. 我试图删除一行,条件是该行在基于同一列的另一行中没有特定值。 (if a CASEID does not have a correlating form 8, delete the CASEID) eg (如果一个CASEID没有相关表格8,则删除该CASEID),例如

Form  CASEID  
7        001  
8        001  
8        001  
7        002  
7        003  
8        003  
8        003  

I have tried to search for an answer to this and haven't been able to find one. 我试图寻找一个答案,但找不到。 I feel like I need an if statement but my co-worker suggested a subset function. 我觉得我需要一个if语句,但是我的同事建议了一个子集函数。 Any help would be appreciated! 任何帮助,将不胜感激!

new_df <- subset(df, Form==8)

The second parameter of the subset function is a logical expression, just like an if statement, as you mentioned. subset函数的第二个参数是逻辑表达式,就像您提到的if语句一样。 Here, we subset rows based on if their form column is equal to 8. 在这里,我们根据行的表单列是否等于8来对行进行子集化。

Here are two solutions that I could think of. 这是我可以想到的两个解决方案。 One using subset and the other with dplyr's inner_join() . 一个使用子集,另一个使用dplyr的inner_join()

The difference between the solutions is that in option 1, duplicate rows and the original order has been retained, and in option 2 duplicate rows have been removed. 解决方案之间的区别在于,在选项1中,保留了重复的行和原始顺序,在选项2中,删除了重复的行。

Solution 1 - using subset and keeping duplicate rows: 解决方案1-使用子集并保留重复的行:

df[df$CASEID %in% subset(df, Form == 8)$CASEID, ] 

The result is: 结果是:

  Form CASEID
1    7      1
2    8      1
3    8      1
5    7      3
6    8      3
7    8      3

Solution 2 - with inner_join() 解决方案2-使用inner_join()

library(dplyr)
subset(df, Form == 8) %>% 
      select(CASEID) %>% 
      inner_join(df) %>% 
      select(Form, CASEID) %>% 
      distinct()

The result is: 结果是:

  Form CASEID
1    7      1
2    8      1
3    7      3
4    8      3

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

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