基本上,我有几个频率表 d1 和 d2。假设我有:
UPDATE2: d1 的实际结构是表。因此 d1 由 获得d1 <- table(datavector)
,对于 d2 类似。
d1
Value 0 1 2 3 4 9
Freq 25 30 100 10 10 10
d2
Value 0 1 3 5 7 11 13
Freq 25 30 100 10 10 10 12
问题:我想生成一个矩阵,其行对应于 d1 和 d2,列对应于 d1 和 d2 中看到的所有不同的“值”。所以我想生成一个矩阵,其行和列如下所示:
[,"0"] [,"1"] [,"2"] [,"3"] [,"4"] [,"5"] [,"7"] [,"9"] [,"11"] [,"13"]
[1,] 25 30 100 10 10 0 0 10 0 0
[2,] 25 30 0 100 0 10 10 0 10 12
请注意,没有列号 6 、 8 和 10 因为它们没有出现在频率表中。最终,我试图将这个矩阵放入一个函数image.plot()
中。
更新 1:我认为我可以允许列号 6,8 和 10 出现在矩阵中,但最终我将不得不编写一个 for 循环来消除仅包含零条目的列。
更新 3:请注意,我实际上正在使用 250 个数据向量,因此有 250 个表(每个表都有不同的长度/维度)。所以,我正在寻找一个有效的解决方案
更新 4:请将以上内容视为我想要实现的目标的摘要。真实数据集如下:
> dput(head(get.dist(fnn[1])))
structure(c(0.999214894571557, 0.000134589502018843, 4.48631673396142e-05,
2.24315836698071e-05, 6.72947510094213e-05, 8.97263346792284e-05,
2.24315836698071e-05, 4.48631673396142e-05, 4.48631673396142e-05,
2.24315836698071e-05, 2.24315836698071e-05, 6.72947510094213e-05,
2.24315836698071e-05, 2.24315836698071e-05, 4.48631673396142e-05,
2.24315836698071e-05, 6.72947510094213e-05, 2.24315836698071e-05
), class = "table", .Dim = 18L, .Dimnames = structure(list(d = c("0",
"1", "2", "3", "4", "5", "8", "9", "11", "12", "15", "16", "17",
"18", "20", "22", "24", "31")), .Names = "d"))
> dput(head(get.dist(fnn[2])))
structure(c(0.71161956034096, 0.199147599820547, 0.0644010767160162,
0.0147599820547331, 0.00327501121579183, 0.000807537012113055,
6.72947510094213e-05, 0.000785105428443248, 0.000179452669358457,
0.000134589502018843, 0.000112157918349035, 4.48631673396142e-05,
6.72947510094213e-05, 0.00307312696276357, 0.00107671601615074,
0.000336473755047106, 6.72947510094213e-05, 2.24315836698071e-05,
2.24315836698071e-05), class = "table", .Dim = 19L, .Dimnames = structure(list(
d = c("0", "1", "2", "3", "4", "5", "6", "9", "10", "11",
"35", "36", "37", "38", "39", "40", "41", "42", "43")), .Names = "d"))
> dput(head(get.dist(fnn[3])))
structure(c(0.747353073126963, 0.13138178555406, 0.0295423956931359,
0.0139075818752804, 0.0119560340960072, 0.0151861821444594, 0.0243382682817407,
0.00697622252131, 0.00255720053835801, 0.00161507402422611, 0.00293853746074473,
0.00116644235082997, 0.004419021982952, 0.0018842530282638, 0.000628084342754598,
0.00053835800807537, 0.000448631673396142, 0.000493494840735756,
0.000650515926424406, 0.000403768506056528, 0.000269179004037685,
0.000179452669358457, 0.000269179004037685, 0.000179452669358457,
8.97263346792284e-05, 0.000246747420367878, 4.48631673396142e-05,
4.48631673396142e-05, 4.48631673396142e-05, 2.24315836698071e-05,
2.24315836698071e-05, 4.48631673396142e-05, 2.24315836698071e-05,
2.24315836698071e-05, 2.24315836698071e-05, 2.24315836698071e-05,
2.24315836698071e-05, 2.24315836698071e-05, 2.24315836698071e-05
), class = "table", .Dim = 39L, .Dimnames = structure(list(d = c("0",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "30", "32", "33", "34", "36", "37",
"38", "43", "54", "67")), .Names = "d"))
> dput(head(get.dist(fnn[4])))
structure(c(0.217743382682817, 0.49416778824585, 0.135150291610588,
0.0331987438313145, 0.0243831314490803, 0.0431135038133692, 0.022790489008524,
0.00912965455361149, 0.00614625392552714, 0.00937640197397936,
0.00244504262000897, 0.000560789591745177, 0.000493494840735756,
0.000448631673396142, 0.000336473755047106, 0.000112157918349035,
0.000201884253028264, 4.48631673396142e-05, 4.48631673396142e-05,
2.24315836698071e-05, 2.24315836698071e-05, 4.48631673396142e-05,
2.24315836698071e-05), class = "table", .Dim = 23L, .Dimnames = structure(list(
d = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "23",
"25", "45")), .Names = "d"))