如何仅返回数据列中任何列单元格包含某些特定字符串的行?

时间:2019-04-23 20:19:27

标签: r string filter dplyr multiple-conditions

我有一个如下数据框,但有384列:

id  col1    col2       col3     col4    col5    .....     col385


1       B45-P   Y   X       RH_B17   S-B45   IU_B34

'
             IU_B34 Y   Y   Y      X

.   S-B45                   RH_B17         X

'
            RH_B17                 X
'
    X   S-B45       X   x   X   IU_B34     X


155 Y   RH_B17              Y       X   

我想过滤上面的数据框,只保留它们包含的任何列(B45或B17或B34)中的行。

I attached the image of the data frame

1 个答案:

答案 0 :(得分:0)

笨重,但对我有用:

library(tidyr)
library(stringr)
df[str_detect(string = unite(df, col = "all", sep = " ")$all, pattern = "B45|B17|B34"),]