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

Aen*_*ung 2 r tm qdap

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

Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE
Run Code Online (Sandbox Code Playgroud)

我认为这是说我需要将数据转换为 aDocumentTermMatrix或 a TermDocumentMatrix,但是由于我只有一个正在处理的列,因此我也无法创建。错误如下:

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

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

> # 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
Run Code Online (Sandbox Code Playgroud)

law*_*yeR 5

如果您使用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
Run Code Online (Sandbox Code Playgroud)