1

我是 R 的新手。我正在挖掘 csv 文件中存在的数据 - 一列中的报告摘要,另一列中的报告日期和第三列中的报告机构。我需要调查与“欺诈”相关的术语如何随时间变化或因机构而异。我过滤了包含“欺诈”一词的行并创建了一个新的 csv 文件。

如何创建一个以年为行、以术语为列的术语频率矩阵,以便我可以查找最高频率术语并进行一些聚类?

基本上,我需要根据年份创建一个术语频率矩阵

Input data: (csv)
**Year**    **Summary** (around 300 words each)    
1945             <text>
1985             <text>
2011             <text>

Desired 0utput : (Term frequency matrix)

       term1     term2    term3  term4 .......
1945     3         5        7       8 .....
1985     1         2        0       7  .....
2011      .            .   .    

Any help would be greatly appreciated.
4

1 回答 1

4

将来请提供一个最小的工作示例。

这并不完全使用 tm 而是使用 qdap ,因为它更适合您的数据类型:

library(qdap)
#create a fake data set (please do this in the future yourself) 
dat <- data.frame(year=1945:(1945+10), summary=DATA$state) 

##    year                               summary
## 1  1945         Computer is fun. Not too fun.
## 2  1946               No it's not, it's dumb.
## 3  1947                    What should we do?
## 4  1948                  You liar, it stinks!
## 5  1949               I am telling the truth!
## 6  1950                How can we be certain?
## 7  1951                      There is no way.
## 8  1952                       I distrust you.
## 9  1953           What are you talking about?
## 10 1954         Shall we move on?  Good then.
## 11 1955 I'm hungry.  Let's eat.  You already?

现在创建词频矩阵(类似于术语文档矩阵):

t(with(dat, wfm(summary, year)))

##      about already am are be ... you
## 1945     0       0  0   0  0       0
## 1946     0       0  0   0  0       0
## 1947     0       0  0   0  0       0
## 1948     0       0  0   0  0       1
## 1949     0       0  1   0  0       0
## 1950     0       0  0   0  1       0
## 1951     0       0  0   0  0       0
## 1952     0       0  0   0  0       1
## 1953     1       0  0   1  0       1
## 1954     0       0  0   0  0       0
## 1955     0       1  0   0  0       1

或者,您可以从qdap 版本 1.1.0创建一个 tru DocumentTermMatrix :

with(dat, dtm(summary, year))

## > with(dat, dtm(summary, year))
## A document-term matrix (11 documents, 41 terms)
## 
## Non-/sparse entries: 51/400
## Sparsity           : 89%
## Maximal term length: 8 
## Weighting          : term frequency (tf)
于 2013-05-21T18:58:57.943 回答