X3            X4            X5            X6            X7            X8            X9
-63.2929      -64.9519      -65.9586      -65.2485      -63.3387      -56.749       -51.7419
-68.7098      -70.2028      -71.329       -70.5252      -68.7843      -63.0799      -58.7491
58.3993       -60.0864      -60.7925      -59.6933      -57.2357      49.8444       44.1056
-57.8334      59.6063       60.3079       -59.1852      -56.6365      -49.2706      -43.5538
-37.6082      -39.6347      -39.6647      38.4396       34.9498       -26.6984      -21.9614
37.2886       39.0565       -38.8722      -37.5427      -33.969       25.9074       21.7712
-61.972       -63.0513      63.5082       -62.2337      59.7711       -54.2189      -51.6613
-60.5783      -61.6254      -61.9812      -60.6696      58.066        -52.4656      -49.9653


从上面的矩阵中,我想定位一个最大和最小单元格为列的单元格,然后对每两个邻居的行进行累加和。例如;对于第1列,最小值为-68.70(标识单元格),然后加(-70.2028和-71.329),以此类推,最小值和最大值相同。我该怎么办?

data=read.table('test.csv', header=TRUE, sep=',')

matrix=data.matrix(data)

最佳答案

这是你想要的? (警告:使用错误的编码做法的“ hack-y”答案)

data = read.table(text = "X3            X4            X5            X6            X7            X8            X9
-63.2929      -64.9519      -65.9586      -65.2485      -63.3387      -56.749       -51.7419
-68.7098      -70.2028      -71.329       -70.5252      -68.7843      -63.0799      -58.7491
58.3993       -60.0864      -60.7925      -59.6933      -57.2357      49.8444       44.1056
-57.8334      59.6063       60.3079       -59.1852      -56.6365      -49.2706      -43.5538
-37.6082      -39.6347      -39.6647      38.4396       34.9498       -26.6984      -21.9614
37.2886       39.0565       -38.8722      -37.5427      -33.969       25.9074       21.7712
-61.972       -63.0513      63.5082       -62.2337      59.7711       -54.2189      -51.6613
-60.5783      -61.6254      -61.9812      -60.6696      58.066        -52.4656      -49.9653", header = T)

sapply(1:(ncol(data)-2), function(x) {c(sum(data[c(which.min(data[,x])),x:(x+2)]),
                                        sum(data[c(which.max(data[,x])),x:(x+2)]))})

          [,1]     [,2]      [,3]      [,4]      [,5]
[1,] -210.2416 -212.057 -210.6385 -202.3894 -190.6133
[2,]  -62.4796   60.729   61.0456   46.6910  -46.1091


第一行最小值,第二行最大值

关于r - 如何累积和行明智?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28055405/

10-12 17:36