Looking to pull back rows where the value in one column exists as a string in another column (within the same row).
I have a df:
A <- c("cat", "dog", "boy")
B <- c("cat in the cradle", "meet the parents", "boy mmets world")
df <- as.data.frame(A, B)
A B
cat cat in the cradle
dog meet the parents
boy boy meets world
I'm trying things like:
df2 <- df %>%
filter(grepl(A, B)) # doesn't work because it thinks A is the whole column vector
df2 <- df %>%
filter(B %in% A) # which doesn't work because it has to be exact
I want it to produce
A B
cat cat in the cradle
boy boy meets world
Thanks in advance!
Matt
We can do this with
Map
Update
Using
tidyverse
, similar option would bepurrr::map2
withstringr::str_detect
data
You can either apply the function to both vectors using
Map
or iterate through the row usingsapply