多列上的R dplyr过滤字符串条件 [英] R dplyr filter string condition on multiple columns
本文介绍了多列上的R dplyr过滤字符串条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 df,例如
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)
如果变量有X",我如何过滤变量 v2 到 v5.我看过一些使用 filter at 的例子,但这些例子似乎只适用于数字条件.
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))
并将 >5 替换为X"不起作用
and replacing >5 for "X" does not work
推荐答案
您可以使用 filter_at
和 any_vars
来选择至少具有一个 值的行X"
.
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
但是,filter_at
已被取代,因此要将其转换为 across
,您可以这样做:
However, filter_at
has been superseeded so to translate this into across
you can do :
df %>% filter(Reduce(`|`, across(v2:v5, ~. == 'X')))
在基础 R 中也更容易:
It is also easier in base R :
df[rowSums(df[-1] == 'X') > 0, ]
这篇关于多列上的R dplyr过滤字符串条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文