使用R中的tm-包创建一个文档-术语矩阵:
dtm <- DocumentTermMatrix(cor, control = list(dictionary=c("someTerm")))
导致了这样的结果:
A document-term matrix (291 documents, 1 terms)
Non-/sparse entries: 48/243
Sparsity : 84%
Maximal term length: 8
Weighting : term frequency (tf)
Terms
Docs someTerm
doc1 0
doc2 0
doc3 7
doc4 22
doc5 0
现在,我想根据文档中someTerm出现的次数来过滤这个文档项矩阵。例如,只过滤someTerm至少出现一次的文档。doc3和doc4在这里。
我怎样才能做到这一点?
发布于 2014-06-14 21:33:54
它非常类似于你如何子集一个正则的R矩阵。例如,要从示例路透社数据集中创建一个文档术语矩阵,其中只有“将”一次以上出现的行:
reut21578 <- system.file("texts", "crude", package = "tm")
reuters <- VCorpus(DirSource(reut21578),
readerControl = list(reader = readReut21578XMLasPlain))
dtm <- DocumentTermMatrix(reuters)
v <- as.vector(dtm[,"would"]>1)
dtm2 <- dtm[v, ]
> inspect(dtm2[, "would"])
A document-term matrix (3 documents, 1 terms)
Non-/sparse entries: 3/0
Sparsity : 0%
Maximal term length: 5
Weighting : term frequency (tf)
Terms
Docs would
246 2
489 2
502 2
tm
文档术语矩阵是来自包slam
的一个简单的三重奏矩阵,因此slam
文档有助于理解如何操作dtms。
发布于 2017-03-20 12:37:39
或者,您可以使用removeSparseTerms函数来删除空元素(查看文档这里)。
dtm <- removeSparseTerms(dtm, 0.1) # This makes a matrix that is 10% empty space, maximum
https://stackoverflow.com/questions/24224298
复制相似问题