r - 用 R 中指定单词旁边的单词过滤所有行

标签 r tidyverse tidyr tidytext

我有一列包含字符串内容

temp <- c(NA, NA, "grocery pantry all offers", NA, "grocery offers today low price", 
"grocery offers today low price", "tide soap", "tide soap bar", 
"tide detergent powders 2kg", NA, "tide", "tide detergent powders 2kg", 
"liquid detergent tide brand")

我的目的是用紧挨着 Tide 的单词创建一个 bigram。为此,我需要过滤掉靠近潮汐的单词。无论是左侧还是右侧。对于上述输出中的 ex 将是
tide soap
tide soap
tide detergent
tide detergent
detergent tide
tide brand

有什么帮助吗?

最佳答案

如果您使用 quanteda 包,这很简单。您指定要定位的单词并决定目标左侧/右侧的单词数量。

library(quanteda)

kwic(x = temp, pattern = "tide", window = 1) %>% 
as.data.frame

  docname from to       pre keyword      post pattern
1   text7    1  1              tide      soap    tide
2   text8    1  1              tide      soap    tide
3   text9    1  1              tide detergent    tide
4  text11    1  1              tide              tide
5  text12    1  1              tide detergent    tide
6  text13    3  3 detergent    tide     brand    tide

关于r - 用 R 中指定单词旁边的单词过滤所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60208260/

相关文章:

R dplyr 总结错误?

r - tidyverse 解决方案,用于将多个列重新编码为新列,其中列名后缀增加一

r - 根据条件为每个 ID 提取多行

r - tidyr::complete 带有可变长度的列名向量

r - 如何按 r 中数据框中的行对数字数据进行排名?

r - 如何使用 rmarkdown 在 pdf 文档中获取内联方程?

r - 如果它们具有相同的开头,如何替换矩阵中的值?

r - mutate(across) 在 tidyverse 中生成多个新列

R/ggplot 2 - 使用 Facet_grid 和 geom histogram/errorbar 处理不均匀的组大小

r - 合并 R 中变量中具有相同值的行