Mads Obi
Mads Obi

Reputation: 560

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).

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!

Upvotes: 1

Views: 900

Answers (1)

akrun
akrun

Reputation: 887163

We can paste the values together

library(tidyerse)
df %>% 
      filter(grepl(str_c("^(", str_c(aList,  collapse="|"), ")"), column))

Upvotes: 2

Related Questions