[英]Removing a group of words from a character vector
Let's say that I have a character vector of random names. 假设我有一个随机名称的字符向量。 I also have another character vector with a number of car makes and I want to remove any occurrence of a car incident in the original vector.
我还有另一个带有许多汽车制造的角色向量,我想要删除原始向量中发生的任何汽车事件。
So given the vectors: 所以给出了矢量:
dat = c("Tonyhonda","DaveFord","Alextoyota")
car = c("Honda","Ford","Toyota","honda","ford","toyota")
I want to end up with something like below: 我想最终得到如下内容:
dat = c("Tony","Dave","Alex")
How can I remove part of a string in R? 如何删除R中的部分字符串?
gsub(x = dat, pattern = paste(car, collapse = "|"), replacement = "")
[1] "Tony" "Dave" "Alex"
Just formalizing 42-'s comment above. 只是将上面的42条评论正式化。 Rather than using
而不是使用
car = c("Honda","Ford","Toyota","honda","ford","toyota")
You can just use: 你可以使用:
carlist = c("Honda","Ford","Toyota")
gsub(x = dat, pattern = paste(car, collapse = "|"), replacement = "", ignore.case = TRUE)
[1] "Tony" "Dave" "Alex"
That allows you to only put each word you want to exclude in the list one time. 这样,您只需将要排除的每个单词放在列表中一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.