How to make a cumulative sum of rows?

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

From the above matrix, I want to find the cell with the maximum and minimum column of the cell value, and then perform the cumulative sum of each of the two adjacent rows. For instance; for column 1, the minimum value is -68.70 (identify cell) and add (-70.2028 and -71.329), etc. for the minimum and the same maximum. How should I do it?

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

matrix=data.matrix(data)
+4
source share
2 answers

Is this what you are looking for? (Warning: hack-y response using bad coding practice)

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

The first line is min, the second line is max.

+3
source
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] )
  }
0
source

All Articles