使用R中的dplyr查找其中一列字符串位于另一列中的行

Mat*_* W. 5 regex r dplyr grepl

试图拉回其中一列中的值作为字符串存在于另一列(同一行中)的行。

我有一个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
Run Code Online (Sandbox Code Playgroud)

我正在尝试类似的事情:

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
Run Code Online (Sandbox Code Playgroud)

我要它产生

A       B
cat     cat in the cradle
boy     boy meets world
Run Code Online (Sandbox Code Playgroud)

提前致谢!

马特

akr*_*run 6

我们可以这样做Map

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

更新

使用,类似tidyverse的选项是purrr::map2stringr::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
Run Code Online (Sandbox Code Playgroud)

数据

df <- data.frame(A, B, stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)


Jas*_*ang 4

您可以使用该函数将函数应用于两个向量Map,也可以使用迭代该行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
Run Code Online (Sandbox Code Playgroud)