Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if string contains all elements from vector? (R, dataframes, dplyr)

Tags:

r

dplyr

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

like image 652
JsDart Avatar asked Sep 12 '25 18:09

JsDart


1 Answers

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
like image 147
eipi10 Avatar answered Sep 14 '25 12:09

eipi10