[英]R: Filtering a data frame by matching partial from a list using "grepl"
I have a large data frame ( df
) I want to filter by searching for partial matches between a column ( df$column
) and a list ( aList
).我有一个大数据框 (
df
) 我想通过搜索列 ( df$column
) 和列表 ( aList
) 之间的部分匹配来过滤。
aList <- c("ID1", "ID2", "ID3")
The variable in my data frame I use for filtering contains values that might only start with the values in the list.我用于过滤的数据框中的变量包含可能仅以列表中的值开头的值。 Example:
ID1_23
or ID2AV
.示例:
ID1_23
或ID2AV
。
I would then like to use grepl
or similar to search for any value in my data frame column that starts with a value from aList
.然后,我想使用
grepl
或类似方法在我的数据框列中搜索以aList
的值开头的任何值。 My approach on handling this manner when searching after only single values would be:我在仅搜索单个值时处理这种方式的方法是:
library(dplyr)
newDf <- df %>% filter(grepl("^ID1", column))
My problem then arises on how to do the similair with all values in my list.然后我的问题出现在如何对我的列表中的所有值进行类似处理。 I have tried the following approach:
我尝试了以下方法:
dummyList <- c()
for (i in 1:length(aList)){
list1 <- dplyr::filter(grepl(paste("\"^", aList[i], "\""), df$column))
rbind(list1, dummyList)
}
which provide me with the follwing error code:这为我提供了以下错误代码:
Error in UseMthod("filter_") :
no applicable method for ´filter_´ applied to an obecjt of class "logical"
Can anyone help me?谁能帮我?
Thanks!谢谢!
We can paste
the values together我们可以将这些值
paste
在一起
library(tidyerse)
df %>%
filter(grepl(str_c("^(", str_c(aList, collapse="|"), ")"), column))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.