Find rows where one column string is in another column using dplyr in R

旧时模样 提交于 2019-12-10 14:55:35

问题


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


回答1:


You can either apply the function to both vectors using Map or iterate through the row using sapply

df %>%
  filter(unlist(Map(function(x, y) grepl(x, y), A, B)))
    A                 B
1 cat cat in the cradle
2 boy   boy mmets world

df %>%
  filter(sapply(1:nrow(.), function(i) grepl(A[i], B[i])))
    A                 B
1 cat cat in the cradle
2 boy   boy mmets world



回答2:


We can do this with Map

df[mapply(grepl, df$A, df$B),]
#    A                 B
#1 cat cat in the cradle
#3 boy   boy mmets world

Update

Using tidyverse, similar option would be purrr::map2 with stringr::str_detect

library(tidyverse)
df %>% 
   filter(map2_lgl(B, A,  str_detect))
#     A                 B
#1 cat cat in the cradle
#2 boy   boy mmets world

data

df <- data.frame(A, B, stringsAsFactors=FALSE)


来源:https://stackoverflow.com/questions/43874565/find-rows-where-one-column-string-is-in-another-column-using-dplyr-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!