I have a dataframe, and in it, a column called names stores strings
name 'Ana, Mari' 'John, Doe' 'Stuart, Matthews'
I have a vector that stores a name in an unknown order. For instance,
v <- c('Ana', 'Mari')
v <- c('Mari', 'Ana')
I want to filter ALL the cells that contain ALL elements in the vector. Does anyone know a function that can do this?
I have this so far, but it's checking if the cells contain ANY of the elements in the cell (don't mind the cells containing extra elements that aren't matched to the vector, but all the elements in the vector should be contained in the cell).
df <- df %>% filter(grepl(vector, col_name))
library(tidyverse)
library(stringr)
df = data_frame(name = c('Ana, Mari','John, Doe', 'Stuart, Matthews'))
v <- c('Mari', 'Ana')
Base R:
df[sapply(strsplit(df$name, split=", "), function(str) all(v %in% str)), ]
name 1 Ana, Mari
tidyverse
:
df %>%
group_by(name) %>%
filter(all(v %in% str_split(name, ", ", simplify=TRUE)))
name 1 Ana, Mari
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With