2

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

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

我认为这是说我需要将我的数据转换为 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')"

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

> # 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
4

2 回答 2

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
于 2015-02-25T16:19:00.667 回答
3

您首先需要一个语料库和术语文档矩阵...

library(tm)
a <- c("hello man", "how's it going", "just fine")
a <- tolower(a)
a <- removePunctuation(a)
a <- removeNumbers(a)
myCorpus <- Corpus(VectorSource(a))
myTDM <- TermDocumentMatrix(myCorpus)
findFreqTerms(myTDM)
于 2015-02-25T15:41:43.800 回答