简体   繁体   English

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

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

My Data我的数据

I have a vector of words, like the below.我有一个单词向量,如下所示。 This is an oversimplification, my real vector is over 600 words:这是一个过度简化,我的真实向量超过 600 个字:

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

I have a dataframe with the below structure:我有一个具有以下结构的 dataframe:

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))

It looks like the below picture如下图所示

样本数据集

My issue我的问题

For each keyword on my vector myvec , I need to go around the dataset and check the columns onetext , cop , text3 , and if I find the keyword on either of those 3 columns, then I need to append the keyword into a new column.对于我的向量myvec上的每个关键字,我需要在数据集周围 go 并检查onetextcoptext3列,如果我在这 3 列中的任何一个上找到关键字,那么我需要将append关键字放入新列。 The result would be as the image as follows:结果将如下图所示:

预期结果

My original dataset is quite large (the last column is the longest), so doing multiple nested loops (which is what I tried) is not ideal.我的原始数据集非常大(最后一列最长),因此执行多个嵌套循环(这是我尝试过的)并不理想。

EDIT: Note that as long as the word appears once in that row, that's enough and should be listed.编辑:请注意,只要该单词在该行中出现一次,就足够了,应该列出。 All keywords should be listed.应列出所有关键字。

How could I do this?我怎么能这样做? I'm using tidyverse, so my dataset is actually a tibble .我正在使用 tidyverse,所以我的数据集实际上是一个tibble

Similar Posts (but not quite)类似的帖子(但不完全)

The following posts are somewhat similar, but not quite:以下帖子有些相似,但不完全是:

Here is how you could achieve the result:以下是实现结果的方法:

  1. create a pattern of the vector创建向量的模式
  2. use mutate across to check the needed columns使用mutate across检查所需的列
  3. if the desired string is detected then extract to a new column !如果检测到所需的字符串,则提取到新列!
myvec <- c("cat", "dog", "bird")

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

library(dplyr)
library(tidyr)
df %>% 
  mutate(across(-id, ~case_when(str_detect(., pattern) ~ str_extract(., pattern)), .names = "new_col{col}")) %>% 
  unite(Match, starts_with('new'), na.rm = TRUE, sep = ',')
     id onetext                cop                                                                                       text3                                                                                                 Match   
  <dbl> <chr>                  <chr>                                                                                     <chr>                                                                                                 <chr>   
1     1 cat furry pink british Little Grey Cat is the nickname given to a kitten of the British Shorthair breed that ro~ On October 4th the first single topic blog devoted to the little grey cat was launched On October 20~ 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                                       dog,cat 
3     3 bird cat issues        A cat and bird can coexist in a home but you will have to take certain measures to ensur~ Some cats will not care about a pet bird at all while others will make it its life mission to get at~ bird,ca~
> library(tidyr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM