【问题标题】:Keep rows with have a specific word [duplicate]保留具有特定单词的行[重复]
【发布时间】:2020-05-25 17:23:18
【问题描述】:

使用此命令,它会保留具有特定单词的行

df[df$ID == "interesting", ]

如果该单词存在于该行中,但它有更多单词围绕如何找到该单词是否存在并保留该行。

示例输入

data.frame(text = c("interesting", " I am interesting for this", "remove")

预期输出

data.frame(text = c("interesting", " I am interesting for this")

【问题讨论】:

  • ?grepldf[grepl("interesting", df$text),] 应该这样做。

标签: r


【解决方案1】:

1.示例数据:

df <- data.frame(text = c("interesting", " I am interesting for this", "remove"),
                 stringsAsFactors = FALSE)

使用基础 R 的解决方案。使用 grepl 的索引:

df[grepl("interesting", df$text), ]

这会返回:

[1] "interesting"                " I am interesting for this"

编辑 1

更改代码,使其返回 data.frame 而不是向量。

df[grep("interesting", df$text), , drop = FALSE]

现在返回:

                        text
1                interesting
2  I am interesting for this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 2019-11-18
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多