简体   繁体   English

R:gsub,匹配并删除

[英]R: gsub, match and remove

I have an dataframe x where 我有一个数据框x

x[1]= "red monkey"
X[2]= "blue whale"
X[3]= "Pink Panther"

and so on...(as it is a big data set) 等等...(因为这是一个大数据集)

color=read.csv("colors.csv")
color[,3]
Blue
Red
White
Grey
Pink
Red
Green

I have to match if X[i] contains any word from color[,3] and if yes, then remove it. 如果X[i]包含color[,3]中的任何单词,我必须匹配,如果是,则将其删除。 ie the result should be another data frame like this 即结果应该是这样的另一个数据帧

y[1]= "monkey"
y[2]= "whale"
y[3]= "Panther"

and so on.. 等等..

How can I do that. 我怎样才能做到这一点。

Thanks in advance. 提前致谢。

Assuming x is a vector, 假设x是一个向量,

gsub(paste(tolower(color[,3]), collapse='|'), '', tolower(x))
#[1] " monkey"  " whale"   " panther"

#to trim the whitespaces,
trimws(gsub(paste(tolower(color[,3]), collapse='|'), '', tolower(x)))
#[1] "monkey"  "whale"   "panther"

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

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