简体   繁体   English

R - 查找向量的字符是否在另一个向量中

[英]R - Find if characters of a vector are in another vector

I have a doubt very similar to this topic here: Find matches of a vector of strings in another vector of strings .我有一个与这里的主题非常相似的疑问: Find matches of a vector of strings in another vector of strings

I have a vector of clients, and if the name indicates that is a commercial client, I need to change the type in my data.frame.我有一个客户向量,如果名称表明它是商业客户,我需要更改我的 data.frame 中的类型。

So, suppose that:所以,假设:

commercial_names <- c("BAKERY","MARKET", "SCHOOL", "CINEMA")
clients <- c("JOHN XX","REESE YY","BAKERY ZZ","SAMANTHA WW")

I tried the code in the topic cited before, but I had an error:我尝试了之前引用的主题中的代码,但出现错误:

> grepl(paste(commercial_names, collape="|"),clients)
[1] TRUE TRUE TRUE TRUE
Warning message:
In grepl(paste(commercial_names, collape = "|"), clients) :
  argument 'pattern' has length > 1 and only the first element will be used

What am I doing wrong?我究竟做错了什么? I would thank any help.我会感谢任何帮助。

Your code is correct but for a typo:您的代码是正确的,但有一个错字:

grepl(paste0(commercial_names, collapse = "|"), clients) # typo: collape
[1] FALSE FALSE  TRUE FALSE

Given the typo, the commercial_names are not collapsed.鉴于错字, commercial_names名称不会折叠。

Not sure how to do this with a one-liner but a loop seems to do the trick.不知道如何用单线做到这一点,但循环似乎可以解决问题。

sapply(clients, function(client) {
  any(str_detect(client, commercial_names))
})
> JOHN XX    REESE YY   BAKERY ZZ SAMANTHA WW 
> FALSE       FALSE        TRUE       FALSE 

I found another way of to do this, with the command %like% of package data.table :我找到了另一种方法,使用命令%like% of package data.table

> clients %like% paste(commercial_names,collapse = "|")
[1] FALSE FALSE  TRUE FALSE

You can do something like this too:你也可以这样做:

clients.first <- gsub(" ..", "", clients)
clients.first %in% commercial_names

This returns:这将返回:

[1] FALSE FALSE  TRUE FALSE

You might need to change the regular expression for gsub if your clients data is more heterogeneous though.如果您的客户端数据更加异构,您可能需要更改gsub的正则表达式。

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

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