R - 如果列包含来自向量的字符串,则将标志附加到另一列

R - If column contains a string from vector, append flag into another column

我的数据

我有一个单词向量,如下所示。这是一个过度简化,我的真实向量超过 600 个单词:

myvec <- c("cat", "dog, "bird")

我有一个具有以下结构的数据框:

structure(list(id = c(1, 2, 3), onetext= c("cat furry pink british", 
"dog cat fight", "bird cat issues"), cop= c("Little Grey Cat is the nickname given to a kitten of the British Shorthair breed that rose to viral fame on Tumblr through a variety of musical tributes and photoshopped parodies in late September 2014", 
"Dogs have soft fur and tails so do cats Do cats like to chase their tails", 
"A cat and bird can coexist in a home but you will have to take certain measures to ensure that a cat cannot physically get to the bird at any point"
), text3 = c("On October 4th the first single topic blog devoted to the little grey cat was launched On October 20th Tumblr blogger Torridgristle shared a cutout exploitable image of the cat, which accumulated over 21000 notes in just over three months.", 
"there are many fights going on and this is just an example text", 
"Some cats will not care about a pet bird at all while others will make it its life mission to get at a bird You will need to assess the personalities of your pets and always remain on guard if you allow your bird and cat to interact"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-3L))

看起来像下图

我的问题

对于我的向量 myvec 上的每个关键字 ,我需要遍历数据集并检查列 onetextcoptext3,如果我在这 3 列中的 either 上找到关键字,那么我需要将关键字 append 到新列中.结果将如下图所示:

我的原始数据集很大(最后一列最长),所以做多个嵌套循环(这是我试过的)并不理想。

编辑:请注意,只要单词在该行中出现 一次,就足够了,应该列出。应列出所有关键字。

我该怎么做?我正在使用 tidyverse,所以我的数据集实际上是 tibble.

相似的帖子(但不完全)

以下帖子有些相似,但又不完全相同:

更新: 如果首选列表:使用 str_extract_all:

df %>%  
  transmute(across(-id, ~case_when(str_detect(., pattern) ~ str_extract_all(., pattern)), .names = "new_col{col}")) 

给出:

  new_colonetext new_colcop new_coltext3
  <list>         <list>     <list>      
1 <chr [1]>      <NULL>     <chr [2]>   
2 <chr [2]>      <chr [2]>  <NULL>      
3 <chr [2]>      <chr [4]>  <chr [5]>  

以下是实现结果的方法:

  1. 创建矢量模式
  2. 使用mutate across检查所需的列
  3. 如果检测到所需的字符串,则提取到新列!
myvec <- c("cat", "dog", "bird")

pattern <- paste(myvec, collapse="|")

library(dplyr)
library(tidyr)
df %>% 
  mutate(across(-id, ~case_when(str_detect(., pattern) ~ str_extract_all(., pattern)), .names = "new_col{col}")) %>% 
  unite(topic, starts_with('new'), na.rm = TRUE, sep = ',')
    id onetext                cop                                                                        text3                                                                              topic                                     
  <dbl> <chr>                  <chr>                                                                      <chr>                                                                              <chr>                                     
1     1 cat furry pink british Little Grey Cat is the nickname given to a kitten of the British Shorthai~ On October 4th the first single topic blog devoted to the little grey cat was lau~ "cat,NULL,c(\"cat\", \"cat\")"            
2     2 dog cat fight          Dogs have soft fur and tails so do cats Do cats like to chase their tails  there are many fights going on and this is just an example text                    "c(\"dog\", \"cat\"),c(\"cat\", \"cat\"),~
3     3 bird cat issues        A cat and bird can coexist in a home but you will have to take certain me~ Some cats will not care about a pet bird at all while others will make it its lif~ "c(\"bird\", \"cat\"),c(\"cat\", \"bird\"~