将来请提供一个最小的工作示例。
这并不完全使用 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)