Skip to content
Prev 166737 / 398502 Next

Help with storage of each matrix generated in a loop

If you are satisfied with the structure of cusumA and just want 999  
more randome realizations of the same, then try creating an empty list  
to hold the 1000 dataframes you are creating and then accumulate  
sequentially to the list.

 > mat <- matrix(data=rep(c(1,2,3,4,5), 16), nrow=16, ncol=5)
 > # The matrix that will be sampled
 > A <- matrix(data=0, nrow=16, ncol=5); cusumA <- list()
##                                     ^new empty list^
 > # The variable that will store the sampled matrix
 > for(i in 1:1000) {
+ # I want to do it 1000 times
+ for(j in 1:nrow(mat)) {
+ # The number of rows to be sampled)
+ A[j,] <- sample(mat[j,])
+ # The sample itself - I want to do it 1000 times and then...
+ cusumA[[i]] <- cumsum(data.frame(A))
# index ^^^^
+ # cumulative sum it, and store the 1000 randomized matrices
+ }}
 >
 > str(cusumA)
List of 1000
  $ :'data.frame':	16 obs. of  5 variables:
   ..$ X1: num [1:16] 5 9 10 14 16 21 22 25 26 30 ...
   ..$ X2: num [1:16] 3 4 6 7 10 11 15 17 20 22 ...
   ..$ X3: num [1:16] 2 4 7 12 17 20 25 30 35 40 ...
   ..$ X4: num [1:16] 4 7 12 15 16 20 23 24 28 31 ...
   ..$ X5: num [1:16] 1 6 10 12 16 18 20 24 26 27 ...
  $ :'data.frame':	16 obs. of  5 variables:
   ..$ X1: num [1:16] 4 5 7 9 14 16 17 22 27 28 ...
   ..$ X2: num [1:16] 3 6 10 11 13 17 20 21 23 28 ...
   ..$ X3: num [1:16] 5 7 10 13 14 19 23 26 27 29 ...
   ..$ X4: num [1:16] 2 7 8 12 15 18 20 24 28 31 ...
   ..$ X5: num [1:16] 1 5 10 15 19 20 25 27 30 34 ...
snip long output (no warnings)