仅将向量R中找到的字保留在数据框中

控制论

我需要从数据框中删除所有非英语单词,如下所示:

ID     text
1      they all went to the store bonkobuns and bought chicken
2      if we believe no exomunch standards are in order then we're ok
3      living among the calipodians seems reasonable  
4      given the state of all relimited editions we should be fine

我想这样结束一个数据框:

 ID     text
 1      they all went to the store and bought chicken
 2      if we believe no standards are in order then we're ok
 3      living among the seems reasonable  
 4      given the state of all editions we should be fine

我有一个包含所有英语单词的向量:word_vec

我可以使用tm包从数据框中删除向量中的所有单词

for(k in 1:nrow(frame){
    for(i in 1:length(word_vec)){
        frame[k,] <- removeWords(frame[i,],word_vec[i])
    }
}

但我想相反。我只想“保留”向量中找到的单词。

多米尼克·科托伊斯

这是一种简单的方法:

txt <- "Hi this is an example"
words <- c("this", "is", "an", "example")
paste(intersect(strsplit(txt, "\\s")[[1]], words), collapse=" ")
[1] "this is an example"

当然,细节在于魔鬼,因此您可能需要稍微调整一下内容,以考虑撇号和其他标点符号。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

仅将配对的行保留在数据框中

仅将单词保留在数据框元素的列表中

如何仅将数字保留在数据框的列的字符串中?

将行号保留在数据框列中

仅将字符串文本保留在R中的另一个数据框中

将第二个条目保留在数据框中

将行保留在数据框中,用于列中值的最后 n 次出现

将记录的最新GROUPR保留在数据框中

根据条件将原始值保留在数据框中

将特定值保留在数据框中并删除所有其他值

如何仅将数字保留在也包含字符串的列数据框中?

如何使用Python将仅连续值保留在Pandas数据框中

如何仅将数字的第一部分保留在数据框中的列中?

将所有数据保留在数据框中,但 2 列中的特定行除外

Python-根据部分字符串匹配将数据行保留在数据框中

跨 n 列抓取不同的行,但将所有列保留在数据框中

仅将列表中包含字符串的元素保留在另一个向量(R)中

使用保留,丢弃和过滤将数据帧保留在数据帧列表中

R在数据框中找到小于另一个向量中的值的值

php:仅将最大值保留在数组中?

如何仅将特定的数组键/值保留在数组中?

如何仅将唯一值保留在数组中?

拆分列表,同时将其他列保留在数据框中

如何汇总行并将其名称保留在数据框中

如何翻转列中包含列名和值的数据框,同时将所有静态列保留在数据框中?

Thymeleaf:提交后将选定的行保留在数据表中

如何将NaN保留在数据透视表中?

GoogleScraper将搜索保留在数据库中

将索引保留在数组中