如果匹配R数据帧中的特定模式,则替换列中的文本

时间:2017-01-11 21:01:34

标签: r dataframe

如果我在数据框中有以下列:

df$column <- c("store", "the store", "factory", "factory A")

如果我们找到包含'store'的行,然后将其替换为df$column,我想在'store'中搜索'store'的匹配项。因此,如果我们遇到'the store',则应将其替换为'store',因为此实例中包含'store'

因此产生的结果将是:

"store", "store", "factory", "factory A"

1 个答案:

答案 0 :(得分:2)

使用grepdf$column[grep("store", df$column)] <- "store"

相关问题