R重新排列数据

时间:2016-08-09 15:28:22

标签: r sparse-matrix

我有一堆由同一个人写的文本,我正在尝试估算他们用于每个文本的模板。我的方式是:

  1. 为所有文字
  2. 创建TermDocumentMatrix
  3. 取每对的原始欧几里德距离
  4. 删除任何大于X距离的对(为了争论而为10)
  5. 压扁森林
  6. 返回每个模板的一个示例,其中包含一些汇总统计信息
  7. 我能够达到距离对的程度,但我无法将dist实例转换为可以使用的实例。 底部有一个可重现的例子。

    dist实例中的数据如下所示:

    dist instance example

    行名和列名对应于原始文本列表中的索引,我可以用它来完成第5步。

    我一直想要摆脱的是一个带有col name, row name, value的稀疏矩阵。

    col, row, value
      1    2  14.966630
      1    3  12.449900
      1    4  13.490738
      1    5  12.688578
      1    6  12.369317
      2    3  12.449900
      2    4  13.564660
      2    5  12.922848
      2    6  12.529964
      3    4   5.385165
      3    5   5.830952
      3    6   5.830952
      4    5   7.416198
      4    6   7.937254
      5    6   7.615773
    

    从这一点开始,我会很乐意切掉比我的截止值更大的所有对并使森林变平,即在此示例中返回3个模板,仅包含文档1的组,仅包含文档2的组和包含文档的第三组3,4,5和6。

    我已经尝试了一些东西,从创建一个矩阵,然后尝试使其稀疏,直接使用dist类中的向量,我似乎无法想象它进行。

    可重复的例子:

    tdm <- matrix(c(1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,2,0,0,0,0,0,1,0,0,0,0,0,3,1,2,2,2,3,2,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,1,2,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,4,1,1,1,1,1,0,0,1,1,1,1,0,1,0,0,0,0,0,0,1,1,1,0,0,1,0,0,0,0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,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,2,1,2,2,2,2,1,0,1,2,1,1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,2,2,2,2,2,2,3,3,4,5,3,1,2,1,1,1,1,1,1,0,0,0,0,3,3,0,0,1,1,0,1,0,0,0,0), nrow=6)
    rownames(tdm) <- 1:6
    colnames(tdm) <- paste("term", 1:229, sep="")
    tdm.dist <- dist(tdm)
    # I'm stuck turning tdm.dist into what I have shown
    

2 个答案:

答案 0 :(得分:2)

将类似“矩阵”的对象转换为[row,col,value]“data.frame”的经典方法是as.data.frame(as.table(.))路由。具体来说,我们需要:

subset(as.data.frame(as.table(as.matrix(tdm.dist))), as.numeric(Var1) < as.numeric(Var2))

但这包括太多的强制和更大的对象的创建只是立即成为子集。

由于dist以“lower.tri”角度形式存储其值,我们可以使用combn生成行/列索引,使用“dist”对象生成cbind:< / p>

data.frame(do.call(rbind, combn(attr(tdm.dist, "Size"), 2, simplify = FALSE)), c(tdm.dist))

此外,“Matrix”软件包具有一定的灵活性,可以在此处使用创建对象的内存效率:

library(Matrix)
tmp = combn(attr(tdm.dist, "Size"), 2)
summary(sparseMatrix(i = tmp[2, ], j = tmp[1, ], x = c(tdm.dist), 
                     dims = rep_len(attr(tdm.dist, "Size"), 2), symmetric = TRUE))

此外,在处理“dist”对象的不同函数中,

cutree(hclust(tdm.dist), h = 10)
#1 2 3 4 5 6 
#1 2 3 3 3 3

通过指定切割高度进行分组。

答案 1 :(得分:1)

这就是我过去使用dplyrtidyr包做过类似的事情。 您可以逐行运行链式(%>%)脚本,以查看数据集如何逐步更新。

tdm <- matrix(c(1,0,0,0,0,0,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1,1,1,0,0,0,0,0,0,0,1,2,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,1,0,2,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,3,0,1,1,1,1,0,0,1,0,1,1,1,0,0,0,0,0,1,0,0,0,0,0,4,2,4,6,4,3,1,0,1,2,1,1,0,1,0,0,0,0,2,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,2,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,2,1,2,2,2,2,1,0,1,2,1,1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,2,2,2,2,2,2,3,3,4,5,3,1,2,1,1,1,1,1,1,0,0,0,0,3,3,0,0,1,1,0,1,0,0,0,0), nrow=6)
rownames(tdm) <- 1:6
colnames(tdm) <- paste("term", 1:229, sep="")
tdm.dist <- dist(tdm)

library(dplyr)
library(tidyr)


tdm.dist %>% 
  as.matrix() %>%                      # update dist object to a matrix
  data.frame() %>%                     # update matrix to a data frame
  setNames(nm = 1:ncol(.)) %>%         # update column names
  mutate(names1 = 1:nrow(.)) %>%       # use rownames as a variable
  gather(names2, value , -names1) %>%  # reshape data
  filter(names1 <= names2)             # keep the values only once

#    names1 names2     value
# 1       1      1  0.000000
# 2       1      2 14.966630
# 3       2      2  0.000000
# 4       1      3 12.449900
# 5       2      3 12.449900
# 6       3      3  0.000000
# 7       1      4 13.490738
# 8       2      4 13.564660
# 9       3      4  5.385165
# 10      4      4  0.000000
# 11      1      5 12.688578
# 12      2      5 12.922848
# 13      3      5  5.830952
# 14      4      5  7.416198
# 15      5      5  0.000000
# 16      1      6 12.369317
# 17      2      6 12.529964
# 18      3      6  5.830952
# 19      4      6  7.937254
# 20      5      6  7.615773
# 21      6      6  0.000000