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)
答案 0 :(得分:3)
这是你在找什么? (警告:使用错误的编码实践回答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
第一行最小,第二行最大
答案 1 :(得分:0)
minsum <- c()
maxsum <- c()
temp <- matrix(sample(c(-1,1),replace=T)*70*runif(70), ncol=7)
for(i in 1:(ncol(temp)-2)){
tmin <- which.min(temp[,i])
tmax <- which.max(temp[,i])
minsum <- c(minsum, temp[tmin,i+1] + temp[tmin,i + 2] )
maxsum <- c(maxsum, temp[tmax,i+1] + temp[tmax,i + 2] )
}