【问题标题】:R: gsub, match and removeR:gsub,匹配和删除
【发布时间】:2016-09-06 13:35:12
【问题描述】:

我有一个数据框x 在哪里

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

等等……(因为它是一个大数据集)

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

如果X[i] 包含来自color[,3] 的任何单词,我必须匹配,如果是,则将其删除。 即结果应该是另一个像这样的数据框

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

等等..

我该怎么做。

提前致谢。

【问题讨论】:

    标签: r string-matching gsub regex-greedy


    【解决方案1】:

    假设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"
    

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 1970-01-01
      • 2015-04-18
      • 2020-09-29
      • 2023-03-10
      • 2013-03-08
      • 1970-01-01
      • 2018-09-13
      • 2018-01-01
      相关资源
      最近更新 更多