[R] Creating a list to store output objects from a recursive loop

Petr PIKAL petr.pikal at precheza.cz
Tue Nov 9 20:24:13 CET 2010


Hi

r-help-bounces at r-project.org napsal dne 09.11.2010 10:26:09:

> "Santosh Srinivas" <santosh.srinivas at gmail.com> 
> Odeslal: r-help-bounces at r-project.org
> 
> 09.11.2010 10:26
> 
> 
> Re: [R] Creating a list to store output objects from a recursive loop
> 
> Figured this out this ways I think
> 
> outPut <- list(list(result1),list(result2))

Hm. I do not think it is the solution.

You shall put

output <- vector("list", 20)
for (i in 1:20){
   output[i] <- niceFunction(x[i],i)
}

and

niceFunction (x,i) {
 
    result1 = someOperations() #zoo object
    result2 = someOperations() #data.frame
 
    result <- list(result1,result2)
 }

Regards
Petr

> 
> -----Original Message-----
> From: Santosh Srinivas [mailto:santosh.srinivas at gmail.com] 
> Sent: 09 November 2010 14:21
> To: 'r-help at r-project.org'
> Subject: Creating a list to store output objects from a recursive loop
> 
> Dear Group,
> 
> I am having a function that I am running in a loop that generated two
> results for each loop
> 
> The result1 is a zoo object
> The result2 is a data frame
> 
> Now I want to put both of them in a list or some structure ... that I 
can
> access or output to a file after the loop is done.
> 
> For e.g.
> 
> for (i in 1:20){
>    niceFunction(x[i],i)
> }
> 
> niceFunction (x,i) {
> 
>    result1 = someOperations() #zoo object
>    result2 = someOperations() #data.frame
> 
>    OutputObj[i,1]=result1
>    OutputObj[i,2]=result2
> 
> }
> 
> How can I go about this?
> 
> Thanks,
> S
> 
> ______________________________________________
> 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