R dplyr filter string condition on multiple columns

吃可爱长大的小学妹 提交于 2021-02-04 21:43:29

问题


I have a df such as

df <-read.table(text="
v1 v2 v3 v4 v5
1  A  B  X  C
2  A  B  C  X
3  A  C  C  C
4  B  D  V  A
5  B  Z  Z  D", header=T)

How can I filter variables v2 to v5 if they have an "X". I've seen some examples using filter at but those seem to work only for numeric conditions.

filter_at(vars(contains("prefix")), all_vars(.>5))

and replacing >5 for "X" does not work


回答1:


You can use filter_at with any_vars to select rows that have at least one value of "X".

library(dplyr)
df %>% filter_at(vars(v2:v5), any_vars(. == 'X'))

#  v1 v2 v3 v4 v5
#1  1  A  B  X  C
#2  2  A  B  C  X

However, filter_at has been superseeded so to translate this into across you can do :

df %>% filter(Reduce(`|`, across(v2:v5, ~. == 'X')))

It is also easier in base R :

df[rowSums(df[-1] == 'X') > 0, ]


来源:https://stackoverflow.com/questions/63643051/r-dplyr-filter-string-condition-on-multiple-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!