簡體   English   中英

使用 R 中的 dplyr 查找一列字符串在另一列中的行

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

希望拉回一列中的值作為另一列(同一行內)中的字符串存在的行。

我有一個 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

提前致謝!

馬特

我們可以用Map做到這一點

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

更新

使用tidyverse ,類似的選項是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

數據

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

您可以使用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

為了完整起見,這可以使用str_detect中的 str_detect 輕松完成

library(tidyverse)

df <- tibble(A, B) %>%
      filter(str_detect(B, fixed(A)) == TRUE)

df
# A tibble: 2 x 2
#   A     B                
#  <chr> <chr>            
#1 cat   cat in the cradle
#2 boy   boy mmets world 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM