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
).
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
.
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
. 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
library(tidyerse)
df %>%
filter(grepl(str_c("^(", str_c(aList, collapse="|"), ")"), column))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.