我使用quanteda包生成一个字频率计数的稀疏矩阵。我想做一个更改,以便输出是很好的,所以简单的1或0,是否存在的单词,但我不知道如何使用稀疏矩阵。
install.packages(quanteda)
示例矩阵
trainingset <- as.dfm(matrix(c(1, 2, 0, 0, 0, 0,
0, 2, 0, 0, 1, 0,
0, 1, 0, 1, 0, 0,
0, 1, 1, 0, 0, 1,
0, 3, 1, 0, 0, 1),
ncol=6, nrow=5, byrow=TRUE,
dimnames = list(docs = paste("d", 1:5, sep = ""),
features = c("Beijing", "Chinese", "Japan", "Macao",
"Shanghai", "Tokyo"))))
答案 0 :(得分:1)
如果查看str(trainingset)
,您可以看到矩阵的插槽。与稀疏矩阵一样,x
槽保存数据,因此您可以使用
trainingset@x <- as.numeric(trainingset@x > 0)
Document-feature matrix of: 5 documents, 6 features (60% sparse).
5 x 6 sparse Matrix of class "dfmSparse"
features
docs Beijing Chinese Japan Macao Shanghai Tokyo
d1 1 1 0 0 0 0
d2 0 1 0 0 1 0
d3 0 1 0 1 0 0
d4 0 1 1 0 0 1
d5 0 1 1 0 0 1