R:TM 包从单列中查找词频

标签 r tm qdap

我最近一直在尝试使用 tm 包在 R 中的 data.frame 中的单个列中查找词频。虽然 data.frame 本身有许多基于数字和字符的列,但我只对一个纯文本列感兴趣。虽然我在清理文本本身时没有遇到问题,但当我尝试使用 findFreqTerms() 命令提取词频时,我收到以下错误:

Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE

我的意思是说我需要将我的数据转换为 DocumentTermMatrixTermDocumentMatrix,但是因为我只有一个列在工作有了,我也无法创建。错误如下:

> Test <- DocumentTermMatrix(Types)
Error in UseMethod("TermDocumentMatrix", x) : 
  no applicable method for 'TermDocumentMatrix' applied to an object of class "c('PlainTextDocument', 'TextDocument')"

有没有办法从单列中获取频率计数?我在下面粘贴了我的完整代码,并对我采取的每个步骤进行了解释。我很感激你们能给我的任何帮助。

> # extracting the single column I wish to analyse from the data frame
  Types <-Expenses$Types
> # lower all cases
  Types <- tolower(Types)
> # remove punctuation
  Types <- removePunctuation(Types)
> # remove numbers
  Types <- removeNumbers(Types)
> # attempting to find word frequency
  findFreqTerms(Types)
Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE

最佳答案

如果使用 qdap 包,您可以直接从文本变量中找到术语的频率:

library(qdap)
a <- c("hello man", "how's it going", "just fine", "really fine", "man o man!")
a <- tolower(a)
a <- removePunctuation(a)
a <- removeNumbers(a)
freq_terms(a) # there are several additional arguments
  WORD   FREQ
1 man       3
2 fine      2
3 going     1
4 hello     1
5 hows      1
6 it        1
7 just      1
8 o         1
9 really    1

关于R:TM 包从单列中查找词频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28722718/

相关文章:

javascript - 在 R Shiny 中的垂直 noUiSliderInput 上将标签自定义为指数文本格式

r - GAM 中的权重选项

从 DocumentTermMatrix 中删除单词

r - 大文本语料库打破了tm_map

r - qdap ngram 极性字典

r - 从字符向量中提取和计算常用词对

r - 混合效应逻辑回归 : different results with MASS and lme4

r - 如何在 R 中使用 strsplit 制作字符串向量?

r - 词云包: get “Error in strwidth(…) : invalid ' cex' value”

在 Azure ML 中未正确检测到 R 包 (qdapTools) 版本