简体   繁体   English

保持符合特定条件的数据框行-扩展

[英]Keep rows of data frame that match certain criteria - EXPANDED

I shortly asked the following question: 我不久问了以下问题:

Keep rows of data frame that match certain criteria 保持符合特定条件的数据框行

I was wondering if it is possible to expand the code provided to say, for instance, give me only those rows where a number in column b appears in at least X out of the 4 letters A,B,C and D? 我想知道是否可以扩展提供的代码,例如,仅给我提供那些行,其中b列中的数字至少出现在4个字母A,B,C和D中的X中? So for instance, the number 5 appears in column a in B,C,D but not in A. However, I would like to return all rows where the number appears in at least 2 out of the 4 letters. 因此,例如,数字5出现在B,C,D的a列中,而不是出现在A的列中。但是,我想返回该数字出现在4个字母中至少2个中的所有行。 That means, the number 5 should be kept plus of course all numbers that appear in all letters. 这就是说,应该保留数字5,当然还要保留所有出现在所有字母中的数字。 Hope it is clear what I am trying to say. 希望我想说的很清楚。

Many thanks in advance! 提前谢谢了!

I find my solution quite easy to expand: 我发现我的解决方案很容易扩展:

keep <- apply( x, 1,
          function( y ) length( which( LETTERS[1:4] %in% x[ x[,2] == y[2], 1 ] ) ) >= 3 )

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

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