簡體   English   中英

使用來自csv文件的2列在R中創建一個術語頻率矩陣?

[英]Create a term frequency matrix using 2 columns from a csv file, in R?

我是R.的新手。我正在挖掘csv文件中的數據 - 一列中的報告摘要,另一列中的報告日期以及thrid列中的報告代理。 我需要調查與“欺詐”相關的條款如何隨着時間的推移發生變化或因代理商而異。 我已經過濾了包含術語“欺詐”的行,並創建了一個新的csv文件。

如何創建一個術語freq矩陣,其中包含多年的行和術語作為列,以便我可以查找最高頻率項並進行一些聚類?

基本上,我需要創建一個與年份相關的術語頻率矩陣

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.

將來請提供一個最小的工作示例。

這不是完全使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM