I am trying to filter a column which contains several keywords (in this example dog and cat) but I am having problems as only the first element is being used.
Here is a dplyr method:
library(stringi)
library(dplyr)
data = data_frame(
id = c(1:7),
type = c("dog1","dog2" ,"cat1","cat2","zebra1", "parrot5", "elephant15")
)
data %>%
filter(animals %>%
paste(collapse = "|") %>%
stri_detect_regex(type, . ) )
Like @Tgsmith61591 mentioned, the pattern argument for the grep function requires a string. Since you're passing in a vector it's warning you that it will only process the first element.
Another solution would be something like this:
dfilter <- unique(grep(paste(filter1, collapse = "|"), df1$type, value=TRUE))
See this post grep using a character vector with multiple patterns
Try this:
dfilter <- df1[sapply(filter1, function(x) grep(x,df1$type)),]
It's complaining because your filter is a vector and grep wants a string.
From this answer:
dfilter <- df1[df1$type %in% grep(paste(filter1, collapse="|"), df1$type, value=TRUE), ]
df1[(gsub('\\d','',df1$type) %in% filter1),]
id type
1 1 dog1
2 2 dog2
3 3 cat1
4 4 cat2
grep
can use |
as an or, so why not paste your filters together with |
as a separator:
dfilter <- df1[grep(paste0(filter1, collapse = "|"), df1$type),]