[R] replace a for loop

Ivan Calandra ivan.calandra at uni-hamburg.de
Wed Feb 10 10:50:49 CET 2010


Hi everybody!

I'm still quite new in R and I don't really understand this whole 
"vectorization" thing.
For now, I use a for loop (and it works fine), but I think it would be 
useful to replace it.

I want to export the result of a test statistic, which is stored as a 
list, into a csv file. I therefore have to export each element of the 
list separately.
Here is the code:
----
str(test)
List of 3
  $ output : num [1:15, 1:6] 1 2 3 4 5 6 7 8 9 10 ...
   ..- attr(*, "dimnames")=List of 2
   .. ..$ : NULL
   .. ..$ : chr [1:6] "con.num" "psihat" "p.value" "p.crit" ...
  $ con    : num [1:6, 1:15] 1 -1 0 0 0 0 1 0 -1 0 ...
  $ num.sig: int 0

for (i in 1:3){
  write.csv(test[[i]], file="filename.csv", append=T, quote=F, row.names=T)
}
----

As I said, I don't completely understand, but I think one of these 
"apply" function might do what I need.

Thanks in advance
Ivan



More information about the R-help mailing list