2

Please excuse my naive question.

I have a loop which returns in every step, a matrix b. I would like to save each matrix from each loop, under a different name depending on the iteration number. For example, at the end of the first iteration, I want to get the matrix named b1, at the end of the second iteration the b2 etc...

As an example, lets use the following code:

count=0
a=matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=6)
for (count in 1:10)  {
  b<-cbind(a[,1],matrix(c( a[sample(nrow(a)),2]), nrow=nrow(a)) ) 
  print(b)
}
count+1

Here, the original matrix is matrix a which has 6 rows and 2 columns. I permute the order of the elements in the second column. The resulting matrix b, is the matrix that conatins as first column the first column of the original matrix a and as second column the permuted second column of a.

Can anyone help me?

Arun
  • 116,683
  • 26
  • 284
  • 387
Danai C.
  • 125
  • 4
  • 8
  • 3
    Do you need them to be separate objects in the workspace? Or would having them all stored in a list be fine? The list option is probably best. – Dason Feb 15 '13 at 15:56
  • As Dason says, a list is a better option. If you *really* need to assign a new variable name, look here: http://stackoverflow.com/a/6034703/509782 – harkmug Feb 15 '13 at 16:00
  • Worth reading: [keep data out of your variable names](http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html). – David Robinson Feb 15 '13 at 16:05
  • Dason,I believe that it is better to get them as separate object. My issue is how to assign the names b1,...,b10 to these matrices, so I can use them latter in my code, independently one each other. – Danai C. Feb 15 '13 at 16:29

3 Answers3

3

You really don't want to store these as separate variables- it would be much better to keep them as a list of 10 matrices. That could be done very easily using replicate:

lst = replicate(10, cbind(a[,1],matrix(c( a[sample(nrow(a)),2]), nrow=nrow(a)) ),
          simplify=FALSE)

You can then access any of the 10 matrices like this:

lst[[1]]
#     [,1] [,2]
#[1,]    1    7
#[2,]    2   10
#[3,]    3   11
#[4,]    4    8
#[5,]    5    9
#[6,]    6   12

Similarly, you could loop over them like this:

for (m in lst) {
    print(m)
    # do something with your matrix m
}
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • But again, the issue of giving a name to each of these list elements remains. I want to give a name lst1 to the lst[[1]], lst2 to the lst[[2]] etc, so I can use the as separate matrices latter. How can I do that in the loop? – Danai C. Feb 15 '13 at 16:59
  • 1
    @DanaiC.: They *are* separate matrices. What do you want to do with ten separate variables that you can't do with this solution? (what's wrong with referring to lst[[1]] instead of referring to lst1?) – David Robinson Feb 15 '13 at 17:00
  • Well, I'm new in programming and my programming causes confusion to other people. So, I'll tell you what's my actual problem. I have a matrix with 747 row and 11 columns. The first column is individuals' ID (I want to keep it as it is) and the rest 10 columns are variables named Y1 to Y10. I want to permute the elements within each of these columns (i.e. permute elements in Y1 column,Y2,...Y10) After that permutation, I need each of these columns separately, to use them in penalized regression. But of course I dont want to do this manually by calling P$Y1, P$Y2 etc. every time, I need a loop. – Danai C. Feb 15 '13 at 17:47
  • @DanaiC.: First, that sounds like a data frame, not a matrix. (You can't access a matrix's column like `P$Y1`. Second, you could simply do a loop over the columns- like `for (j in 2:11)`, and access the column as `P[, j]`. I don't see what that has to do with your question. 3) You might have an easier time if you turned the ID column into rownames- that could be done as `rownames(P) = P[, 1]; P = P[, -1]`. That way the IDs would still stay with the rows but you could treat all columns identically. – David Robinson Feb 15 '13 at 18:01
1

As told before, list is a better option. But, if you still want to save each interation on different variables, you can use assign()

count=0
a=matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=6)
for (count in 1:10)  {
  assign(paste('b',count,sep=''),cbind(a[,1],matrix(c( a[sample(nrow(a)),2]), nrow=nrow(a))))
}
b1
b2
Rcoster
  • 3,170
  • 2
  • 16
  • 35
1

As said in other option it is better to use a list. Here a version using sapply to get pretty named result:

 res <- sapply(paste('b',1:10,sep=''), 
           function(x) cbind(a[,1],matrix(c( a[sample(nrow(a)),2]), nrow=nrow(a)) ),
       simplify=F)

Then To get matrix b5 for example,

res$b5

   [,1] [,2]
[1,]    1    9
[2,]    2    7
[3,]    3    8
[4,]    4   11
[5,]    5   10
[6,]    6   12
agstudy
  • 119,832
  • 17
  • 199
  • 261