【问题标题】:grepl: Search within a string that does not contain a patterngrepl:在不包含模式的字符串中搜索
【发布时间】:2012-01-17 16:52:00
【问题描述】:

似乎grep 有一个反转参数,而grepl 没有。

我想使用 2 个过滤器进行子集化

data$ID[grepl("xyx", data$ID) & data$age>60]

如何为年龄>60 和 ID不包含“xyx”设置子集?我做的是

data$ID[abs(grepl("xyx", data.frame$ID)-1) & data$age>60]

这显然有效,但看起来很糟糕且不直观。有更好的解决方案/论据吗?

谢谢

【问题讨论】:

    标签: r


    【解决方案1】:

    grepl 返回一个逻辑向量。如果您想要相反的结果,可以使用! 运算符。

    data$ID[!grepl("xyx", data$ID) & data$age>60]
    

    【讨论】:

    猜你喜欢
    • 2011-05-25
    • 2019-09-07
    • 2020-05-01
    • 2021-06-12
    • 2019-05-17
    • 2013-06-26
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多