【问题标题】:Use gsub in R to remove some rows based on some pattern在 R 中使用 gsub 根据某些模式删除一些行
【发布时间】:2018-06-22 14:54:39
【问题描述】:

我有一个包含一些文本列的数据表。我想删除那些有 MN 后跟一些数字的行。例如 MN 894080/901060/905034,MN 90706等

library(data.table)
dt<-data.table(
variable= c(
"MN 894080/901060/905034 - a file has some text.",
"L2 BLOCK AMER] [VVol MN 941737][DU MN 934010] a file has some text",
"MN 907068 || bdheks;",
"MN#287627/901060/905034 a file has some text ",
"MN# 944179 || a file has some text",
"(MN #927427)a file has some text",
"MN 933281 - a file has some text",
"a file has some text",
" a file has some text Mnuq"))

最终输出应如下所示:

dt
  variable
a file has some text
a file has some text Mnuq

【问题讨论】:

    标签: r regex data.table gsub


    【解决方案1】:

    grepl 解决方案

    dt[!grepl("MN.*\\d", dt$variable)]
    #                      variable
    # 1:       a file has some text
    # 2:  a file has some text Mnuq
    

    【讨论】:

    • [范围内的dt不需要获取变量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 2018-11-20
    相关资源
    最近更新 更多