基于频率表的子集/过滤器

时间:2017-06-30 01:49:56

标签: r qdap

我有一些带有一些文本数据的df,例如

words <- data.frame(terms = c("qhick brown fox",
                              "tom dick harry", 
                              "cats dgs",
                              "qhick black fox"))

我已经能够根据包含拼写错误的任何行进行分组:

library(qdap)
words[check_spelling(words$terms)$row,,drop=F]

但鉴于我有很多文本数据,我只想过滤更频繁发生的拼写错误:

> sort(which(table(which_misspelled(toString(unique(words$terms)))) > 1), decreasing = T)
qhick 
    2 

所以我现在知道那个&#34; qhick&#34;是一种常见的拼写错误。

我怎么能根据这个表来对单词进行子集化?因此,只返回包含&#34; qhick&#34;?

的行

1 个答案:

答案 0 :(得分:1)

单词本身就是sort()函数的名称。如果您只有一个名字,那么:

top_misspelled <- sort(which(table(which_misspelled(toString(unique(words$terms)))) > 1), decreasing = T)

words[grepl(names(top_misspelled), words$terms), , drop = F]
#            terms
#1 qhick brown fox
#4 qhick black fox

但是如果你有多个,你可以将它们一起折叠以构建grepl查找,如:

words[grepl(paste0(names(top_misspelled), collapse = "|"), words$terms), ,drop = F]

非正则表达式选项也可以将每一行拆分为单词,然后如果行中的任何单词与您感兴趣的字符串匹配,则返回该行:

words[sapply(strsplit(as.character(words[,"terms"]), split=" "), function(x) any(x %in% names(top_misspelled))),
      ,drop = F]

#            terms
#1 qhick brown fox
#4 qhick black fox
相关问题