将表转换为矩阵以进行聚类分析

时间:2018-05-24 09:26:17

标签: r matrix cluster-analysis distance correlation

所以我有一个表告诉两个变量(V1和V2)的freq(N)出现在一起。这是一个示例:

> dput(ans)
structure(list(V1 = c(2L, 7L, 7L, 7L, 7L, 7L, 9L, 9L, 9L, 10L, 
10L, 11L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 
14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 20L, 
20L, 21L, 25L, 29L, 29L, 29L, 33L, 35L, 38L, 42L, 46L, 46L, 46L, 
46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 47L, 47L, 48L, 52L, 
52L, 52L, 52L, 52L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 57L, 57L, 
57L, 57L, 57L, 57L, 58L, 58L, 58L, 58L, 58L, 59L, 59L, 59L, 59L, 
60L, 60L, 60L, 61L, 61L, 62L, 65L, 65L, 65L, 65L, 67L, 67L, 67L, 
68L, 70L, 70L, 71L, 73L, 73L, 74L), V2 = c(3L, 8L, 20L, 21L, 
22L, 78L, 10L, 11L, 12L, 11L, 12L, 12L, 38L, 39L, 14L, 15L, 16L, 
17L, 18L, 29L, 64L, 15L, 16L, 17L, 18L, 16L, 17L, 18L, 17L, 18L, 
29L, 30L, 18L, 29L, 30L, 21L, 22L, 22L, 26L, 30L, 47L, 64L, 34L, 
36L, 39L, 43L, 47L, 48L, 49L, 52L, 65L, 67L, 70L, 71L, 72L, 73L, 
74L, 75L, 48L, 49L, 49L, 65L, 67L, 73L, 74L, 75L, 57L, 58L, 59L, 
60L, 61L, 62L, 63L, 58L, 59L, 60L, 61L, 62L, 63L, 59L, 60L, 61L, 
62L, 63L, 60L, 61L, 62L, 63L, 61L, 62L, 63L, 62L, 63L, 63L, 67L, 
73L, 74L, 75L, 73L, 74L, 75L, 69L, 71L, 72L, 72L, 74L, 75L, 75L
), N = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)),
 row.names = c(NA, -108L), class = c("data.table", "data.frame"))

我想将它转换为696x696矩阵,其中我有V1和V2作为行和列(行和列中的1-696),N作为值。 V1和V2表示我的数据集中的材料。如果表中不存在V1和V2组合,则该值应为0.这是因为我想使用具有质心函数的hclust基于它们出现在一起的频率来聚类材料。

编辑:我只能提供预期输出的示例,这是我正在关注的文章中的图片: enter image description here

2 个答案:

答案 0 :(得分:0)

这对于栅格来说是一项常见的任务...使用栅格包并将其转换回矩阵可能不是最快的解决方案,但它适用于您的测试数据(此处命名为df) ...

library(raster)

r <- raster(nrow=696, ncol=696, crs = NA,
            xmn = 0, xmx = 696, ymn = 0, ymx = 696)
# some indexing corrections
new_xy <- cbind(df[, 2] - 1, 697 - df[, 1])
cells <- cellFromXY(r, new_xy)
r[] <- 0
r[cells] <- unlist(df[, 3])
r <- as.matrix(r)

然后我们可以检查str(r)它是一个696x696数字,max(r)是一个值3,正如预期的那样。另外,r[2, 3] = 1

答案 1 :(得分:0)

要复制您添加到原始问题中的图片,我会执行以下操作:

# convert your contingency table to the appropriate matrix
M <- sparseMatrix(df$V1, df$V2, x = df$N, dims = c(696, 696))
M <- as.matrix(M)
rownames(M) <- 1:696
colnames(M) <- 1:696

有许多格式化选项可用于显示图像矩阵,但要开始,请尝试:

View(M)

enter image description here