[R] Help with storage of each matrix generated in a loop

David Winsemius dwinsemius at comcast.net
Mon Jan 12 18:55:15 CET 2009


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)

-- 
David Winsemius


On Jan 12, 2009, at 12:07 PM, rafamoral wrote:

>
> I need to store each matrix generated in a loop.
>
> I've been working with the CUSUM algorithm and I've been trying to  
> implement
> it in R.
> What I need to do with my dataset is to create 1000 randomized  
> datasets and
> cumulative sum them all and store all of those randomized CUSUMed  
> datasets
> for further analysis and creation of the simulation envelope in the  
> CUSUM
> chart. But I can't manage to store all of them, the script I've  
> written only
> stores the last randomized matrix in the variable I called "cusumA".
>
> Here's the script I've written:
>
> 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)
> # 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 <- cumsum(data.frame(A))
> # cumulative sum it, and store the 1000 randomized matrices
> }}
>
> I've already tried to store it in a null matrix but it will only  
> store the
> first column of each matrix, so all I got was 1000 first columns.  
> The code I
> used to do it was
>
> mat <- matrix(data=rep(c(1,2,3,4,5), 16), nrow=16, ncol=5)
> A <- matrix(data=0, nrow=16, ncol=5)
> cusumA <- matrix()
> for(i in 1:1000) {
> for(j in 1:nrow(mat)) {
> A[j,] <- sample(mat[j,])
> cusumA[i] <- cumsum(data.frame(A))
> }}
>
> and I even got 50+ warnings...
> Any help'd be really appreciated!
> -- 
> View this message in context: http://www.nabble.com/Help-with-storage-of-each-matrix-generated-in-a-loop-tp21418758p21418758.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list