希望拉回一列中的值作为另一列(在同一行内)中的字符串存在的行。
我有一个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
我尝试过这样的事情:
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
我希望它能够产生
A B
cat cat in the cradle
boy boy meets world
提前致谢!
马特
答案 0 :(得分:3)
我们可以使用Map
df[mapply(grepl, df$A, df$B),]
# A B
#1 cat cat in the cradle
#3 boy boy mmets world
使用tidyverse
,类似选项为purrr::map2
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
df <- data.frame(A, B, stringsAsFactors=FALSE)
答案 1 :(得分:1)
您可以使用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
{{1}}