【问题标题】:Importing a Term Document Matrix in CSV format into R将 CSV 格式的术语文档矩阵导入 R
【发布时间】:2017-09-13 17:52:12
【问题描述】:

所以我已经有一个 TDM,但它在 excel 上。所以我将它保存为 CSV。现在我想做一些分析,但我不能使用 tm 包将它作为 TDM 加载。我的 CSV 看起来像这样:

           item01    item02    item03     item04


red         0          1         1           0
circle      1          0         0           1
fame        1          0         0           0
yellow      0          0         1           1 
square      1          0         1           0 

所以我无法将该文件作为 TDM 加载,到目前为止我尝试过的最好的方法是:

myDTM <- as.DocumentTermMatrix(df, weighting = weightBin)

但它在所有单元格上加载 1

<<DocumentTermMatrix (documents: 2529, terms: 1952)>>
Non-/sparse entries: 4936608/0
Sparsity           : 0%
Maximal term length: 27
Weighting          : binary (bin)
Sample             :

             Terms
Docs            item01 item02 item03 item04
      Red        1        1     1       1                
      Circle     1        1     1       1          
      fame       1        1     1       1   

我尝试过先转换为 Corpus 和其他东西,但如果我尝试使用像 inspect(tdm) 这样的任何函数,它会返回一个错误,比如这个或类似的。

Error in `[.simple_triplet_matrix`(x, docs, terms) :

我真的不相信没有办法以正确的格式导入它,有什么建议吗?提前致谢。

【问题讨论】:

    标签: r csv text-mining text-analysis


    【解决方案1】:

    首先尝试将 CSV 转换为稀疏矩阵。我的 CSV 与您的不同,因为我自己输入了它,但想法相同。

    > library(tm)
    > library(Matrix)
    > myDF <- read.csv("my.csv",row.names=1,colClasses=c('character',rep('integer',4)))
    > mySM <- Matrix(as.matrix(myDF),sparse=TRUE)
    > myDTM <- as.DocumentTermMatrix(mySM,weighting = weightBin)
    > inspect(myDTM)
    
    <<DocumentTermMatrix (documents: 5, terms: 4)>>
    Non-/sparse entries: 7/13
    Sparsity           : 65%
    Maximal term length: 6
    Weighting          : binary (bin)
    Sample             :
            Terms
    Docs     item01 item02 item03 item04
      circle      1      1      0      0
      fame        1      0      0      0
      red         0      0      0      0
      square      1      0      1      0
      yellow      0      0      1      1 
    > 
    

    【讨论】:

      猜你喜欢
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 2021-07-27
      • 2018-05-04
      • 1970-01-01
      相关资源
      最近更新 更多