[R] Loop

Milan Bouchet-Valat nalimilan at club.fr
Wed Feb 22 14:51:14 CET 2012


Le mercredi 22 février 2012 à 05:40 -0800, Florian Weiler a écrit :
> Thanks for the answer, and sorry if I was not clear.
> 
> So I run the data imputation using mice with 10 chains and then I get a
> mids-object. From that object I can then extract 10 data sets using the
> complete(imp, n) command (with n=c(1:10)).
> 
> Now I can type this out 10 times:
> set1 <- complete(imp, 1) 
> set2 <- complete(imp, 2)
> etc.
> 
> Each of these set1 to set10 will be a data frame. That all is easy enough,
> but I would like to do this in a loop and create the same 10 data sets. 
> 
> If I use the command proposed by you, then the loop works but again the 10
> data sets are stored in only one single object (a list) when I would like to
> create 10 separate data sets. 
With the suggested solution, you get a list of data sets. You can access
each data set separately as if it was a separate object, e.g. using
set[[1]]. It's usually more practical to have all objects in a list,
this allows you to run actions on all of them easily using lapply() and
things like that.

Hope this helps



More information about the R-help mailing list