我有一个矩阵mat[n,m]
,我想确定最接近mat
点(所有行)的质心的点(即行)。我想出了这个:
centr <- scale(mat, center = TRUE, scale = apply(mat, 2, max))
Fm <- apply(centr ,1,function(x) sum(sqrt(x^2)))
which.min(Fm)
它有效,但也许有更高效,也许是准确的方法吗?感谢
mat示例:
> dput(head(mat))
structure(list(fy = c(304.160180432, 393.125213727, 355.912969482,
350.088253155, 351.956148821, 490.61654591), eu = c(0.296086251326,
0.162222711872, 0.151452641159, 0.23961169977, 0.132601648268,
0.137074975573), imp_local = c(480.202750814, 331.441037491,
203.050453759, 456.505085425, 430.00327848, 277.230868593), imp_global = c(347.135107518,
161.54795769, 157.983395516, 443.870737726, 123.142549285, 315.239271761
), teta1c = c(0.0252401599408, 6.57956488692e-04, 0.0310074099737,
0.0170228762028, 0.0195515308428, 0.0391776269579), k2_2L = c(44518657.5924,
71620854.1301, 50902610.4991, 84002584.625, 43929609.9925, 34962468.9843
), k2_3L = c(52128086.4128, 128335531.617, 125679699.438, 114226656.116,
110931499.851, 69754622.2773), k2_4L = c(110253548.378, 114340569.933,
116906563.095, 50567101.6777, 49713569.6594, 101813179.037),
dfactorc = c(0.960493226986, 1.50308974945, 2.84613429881,
2.31861226179, 2.16659999049, 2.34778059174), teta1s = c(0.0395107110638,
0.00962847626792, 0.00479991540446, 0.0169169865608, 0.0207107324046,
0.0234741510345), k2_ab1s = c(248750767.533, 199591541.246,
299978849.972, 250790926.645, 253980498.772, 271249357.657
), k2_ab2s = c(103009056.854, 123130790.491, 213375004.715,
243337785.573, 223612467.604, 217430726.828), k2_ab3s = c(38666758.325,
34935381.2664, 41527623.8535, 43003841.6305, 36671415.4616,
39634499.5836), dfactors = c(1.31023571455, 1.20547824262,
1.83142594455, 0.578164677995, 2.78456355896, 2.12180398172
), teta1f = c(0.012014295911, 0.0129204486301, 0.0187328110645,
0.0362511360729, 0.012267521498, 0.0217499329378), k2f = c(56690604.1132,
46031000.4443, 45365247.623, 62040573.3939, 69625743.0835,
72350904.8917), dfactorf = c(2.59057644371, 2.45065026752,
0.99909696465, 2.77215102478, 2.39085531534, 0.759336109346
), dfactorb = c(1.63076878799, 2.65806207972, 1.88207747651,
0.58615340105, 2.86670978986, 2.32439426356), roti = c(19.1639753068,
8.01335554277, 1.46039226043, 1.80847686316, 1.64345384545,
18.513039117), rotmaxbp = c(0.215171126018, 0.148433054469,
0.275134892031, 0.499771947677, 0.475639290002, 0.410770977875
), IR2 = c(0.1171280441, 0.0901673506, 0.1438223753, 3.256136e-04,
0.107294361, 0.1523085127)), .Names = c("fy", "eu", "imp_local",
"imp_global", "teta1c", "k2_2L", "k2_3L", "k2_4L", "dfactorc",
"teta1s", "k2_ab1s", "k2_ab2s", "k2_ab3s", "dfactors", "teta1f",
"k2f", "dfactorf", "dfactorb", "roti", "rotmaxbp", "IR2"), row.names = c(NA,
6L), class = "data.frame")
答案 0 :(得分:2)
这可能会这样做。无论如何,它适用于小型测试用例。首先,计算质心,它是所有点的矢量平均值。
centroidval <- colMeans(data_frame)
alldist <- dist(rbind(centroidval,data_frame))
然后输出距离矩阵中的最小值(相对于第一行,即质心)将识别最近的点。