tm package:在矩阵中输出findAssocs()而不是R中的列表

时间:2014-09-24 03:59:31

标签: r matrix tm term-document-matrix

考虑以下列表:

library(tm)
data("crude")
tdm <- TermDocumentMatrix(crude)
a <- findAssocs(tdm, c("oil", "opec", "xyz"), c(0.7, 0.75, 0.1))

如何在列中显示包含与这3个字相关联的所有字词的数据框,并显示:

  1. 相应的相关系数(如果存在)
  2. NA如果这个单词不存在(例如情侣(油,他们)会显示NA)

2 个答案:

答案 0 :(得分:2)

以下是使用reshape2帮助重塑数据的解决方案

library(reshape2)
aa<-do.call(rbind, Map(function(d, n) 
    cbind.data.frame(
      xterm=if (length(d)>0) names(d) else NA, 
      cor=if(length(d)>0) d else NA, 
      term=n),
    a, names(a))
)

dcast(aa, term~xterm, value.var="cor")

答案 1 :(得分:2)

或者您可以使用dplyrtidyr

 library(dplyr)
 library('devtools')
 install_github('hadley/tidyr')

 library(tidyr)

 a1 <- unnest(lapply(a, function(x) data.frame(xterm=names(x),
                cor=x, stringsAsFactors=FALSE)), term)


  a1 %>% 
     spread(xterm, cor) #here it removed terms without any `cor` for the `xterm`
  #  term 15.8 ability above agreement analysts buyers clearly emergency fixed
  #1  oil 0.87      NA  0.76      0.71     0.79   0.70     0.8      0.75  0.73
  #2 opec 0.85     0.8  0.82      0.76     0.85   0.83      NA      0.87    NA
  #  late market meeting prices prices. said that they trying who winter
  #1  0.8   0.75    0.77   0.72      NA 0.78 0.73   NA    0.8 0.8    0.8
  #2   NA     NA    0.88     NA    0.79 0.82   NA  0.8     NA  NA     NA

更新

 aNew <- sapply(tdm$dimnames$Terms, function(i) findAssocs(tdm, i, corlimit=0.95))
 aNew2 <- aNew[!!sapply(aNew, function(x) length(dim(x)))]
 aNew3 <- unnest(lapply(aNew2, function(x) data.frame(xterm=rownames(x), 
                     cor=x[,1], stringsAsFactors=FALSE)[1:3,]), term)
  res <- aNew3 %>% 
              spread(xterm, cor) 

  dim(res)
  #[1] 1021  160

   res[1:3,1:5]
    #     term ... 100,000 10.8 1.1
    #1     ...  NA      NA   NA  NA
    #2 100,000  NA      NA   NA   1
    #3    10.8  NA      NA   NA  NA