7
I have the following situation :
In a numeric type Matrix :
Temp <- matrix(runif(25, 10, 100),nrow=5,ncol=5)
V1 V2 V3 V4 V5
11 34 45 54 55
16 21 45 75 61
88 49 85 21 22
12 13 12 11 10
69 45 75 78 89
How to transform this Matrix into a Matrix that is the accumulated sum of the columns ? The result would be the following
V1 V2 V3 V4 V5
11 45 90 144 199
16 37 82 157 218
88 137 222 243 265
12 25 37 48 58
69 114 189 267 356
I achieved the goal using a for loop, but I believe that there should be a more efficient way to do it since I am working with a Matrix of 2580 lines by 253 columns and this taking a little to generate the result
Temp <- matrix(runif(25, 10, 100),nrow=5,ncol=5)
Temp <- round(Temp,0)
sum_matrix <- matrix(0,nrow=nrow(Temp),ncol=ncol(Temp))
sum_matrix[,1] <- Temp[,1]
for (n in 2:nrow(Temp)) {
sum_matrix[,n] <- sum_matrix[,n-1] + Temp[,n]
}