繁体   English   中英

部分字符串匹配两列R.

[英]Partial string match two columns R

我一直在尝试基于两列共有的正则表达式列表来部分匹配两列内容:

dats<-data.frame(ID=c(1:3),species=c("dog","cat","rabbit"),
species.descriptor=c("all animal dog","all animal cat","rabbit exotic"),product=c(1,2,3),
product.authorise=c("all animal dog cat rabbit","cat horse pig","dog cat"))

为了实现这一目标:

goal<-data.frame(ID=c(1:3),species=c("dog","cat","rabbit"),
            species.descriptor=c("all animal dog","all animal cat","rabbit exotic"),
            product=c(1,2,3),product.authorise=c("all animal dog cat rabbit","cat horse pig",
            "dog cat"), authorised=c("TRUE","TRUE","FALSE"))    

所以为了进一步解释,如果'dog'出现在两列中的任何一点,那么在$ match中这将被视为'TRUE' - 这将适用于任何单个物种描述符。如果没有找到匹配,那么返回FALSE或na会没事的。

到目前为止,我已经达到了这一点:

library(stringr)
patts<-c("dog","cat","all animal")
reg.patts<-paste(patts,collapse="|")
dats$matched<-ifelse((str_extract(dats$species.descriptor,reg.patts) == str_extract(dats$product.authorise,reg.patts)),"TRUE","FALSE")
dats
  ID species species.descriptor product         product.authorise matched
   1     dog     all animal dog       1 all animal dog cat rabbit    TRUE
   2     cat     all animal cat       2             cat horse pig   FALSE
   3  rabbit      rabbit exotic       3                   dog cat    <NA>

正如您所看到的,这正确地标识了第一行和最后一行,因为“所有动物”在两个字符串中首先出现,并且在最后一行中根本没有匹配。 但是,当reg exp没有首先出现在字符串中时,似乎很难(如第二行)。 我已经尝试过str_extract_all,但到目前为止只导致错误消息。 我想知道是否有人可以提供帮助,拜托?

这是使用dplyr进行管道处理的解决方案。 芯组分是使用grepl为逻辑字符串匹配species中都species.descriptorproduct.authorised

library(dplyr)
dats %>%
rowwise() %>%
mutate(authorised = 
           grepl(species, species.descriptor) & 
           grepl(species, product.authorise)
       )

Source: local data frame [3 x 6]
Groups: <by row>

     ID species species.descriptor product         product.authorise authorised
  (int)  (fctr)             (fctr)   (dbl)                    (fctr)      (lgl)
1     1     dog     all animal dog       1 all animal dog cat rabbit       TRUE
2     2     cat     all animal cat       2             cat horse pig       TRUE
3     3  rabbit      rabbit exotic       3                   dog cat      FALSE

如果你真的喜欢stringr你可以使用str_detect函数获得更加用户友好的语法。

library(stringr)
dats %>%
mutate(authorised = 
           str_detect(species.descriptor, species) & 
           str_detect(product.authorise, species)
       )

如果您不喜欢dplyr ,可以直接添加列

dats$authorised <- 
    with(dats, 
         str_detect(species.descriptor, species) & 
             str_detect(product.authorise, species)
         )

暂无
暂无

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

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