从代理包到数据帧的距离矩阵

时间:2017-03-30 16:03:09

标签: r

我有this代码

的距离矩阵

我想将distanceMatrix转换为数据帧。我用这个:

library(reshape2)
melt(distanceMatrix)

as.data.frame(distanceMatrix)

我收到此错误:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ""crossdist"" to a data.frame

数据

distanceMatrix <-
structure(c(1.1025096478618, 2.48701192612548, 1.81748937453859, 
0.68928345814907, 3.4194165172611, 1.39021901561926, 0.696405607391678, 
1.09511501308162, 0.733071057157832, 0.894074317336616, 0.274302486490285, 
2.00790247099612, 2.03702210657379, 0.790303515570192, 0.76573433957666, 
1.0571870370502, 2.08607605440225, 1.18691928628668, 0.950127106192438, 
1.90183580897689, 1.06791623757733, 1.95426617861089, 1.28359907050968, 
0.639828869115434, 1.2125883228325, 1.17334881171837, 2.86424081724093, 
4.29579721901031, 2.48106485650871, 2.47992202769688, 4.78094585963798, 
3.08269692108197, 2.51054397059837, 2.78351950724781, 1.9552995309483, 
1.02672164296738, 2.04833064878561, 2.40777909325915, 1.37714830319657, 
2.54290296394426, 1.99486295133513, 1.42661425293529, 2.75973709232752, 
0.632464187558431, 2.64349038129557, 3.04900615202494, 1.34349249286485, 
0.66548291586285, 1.14201671902258, 2.20314775706901, 3.027560891124, 
2.58016468923376, 0.701837450761437, 1.82650318310107, 1.17318969224049, 
0.898229996978744, 2.04804918964036, 0.510384590416117, 1.20067408397491, 
0.479351971313752, 0.900264653292786, 2.17660319096498, 1.11774249289539, 
1.50312712068438, 2.35380779446751, 0.74568873241509, 0.860144296532242, 
1.49609968893816, 1.27903173482324, 2.30242237929782, 0.546178045451667, 
0.696804454166844, 1.57330737370915, 3.18912158434627, 2.63481498585198, 
0.743304574607114, 1.2813138290548, 0.278296684614969), .Dim = c(26L, 
3L), .Dimnames = list(c("a", "b", "c", "d", "e", "f", "g", "h", 
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
"v", "w", "x", "y", "z"), c("A", "B", "C")), class = "crossdist", method = "Euclidean", call = proxy::dist(x = voterIdealPoints, 
    y = candidateIdealPoints))

1 个答案:

答案 0 :(得分:0)

使用

as.dataframe(as.matrix(distanceMatrix))