Hello: I have a list of data frames, built like this: the second df is a result of a function applied to the first, and so on. So the ith df is always dependent on the (i-1)th df. I've been doing this using for loops. However I think I have too many for loops which is making my code run slowly. Is there any workaround this? How can I avoid the use of for loops? As an example: output.list <- as.list(rep("", 100))#creation of a list output.list[[1]] <- df1#first position for(I in 2:100){#following positions df0 <- output.list[[i-1]] df0_1 <- f1(df0)#function applied to the previous df output.list[[i]] <- df0_1#new df } thanks, Frederico [[alternative HTML version deleted]]
You might want to use Rprof to profile your code to understand where the time is going; it might be in the function you are callingband therefore the "for" loop might not be the issue. Sent from my iPad On Jun 27, 2013, at 4:19, "Frederico Mestre" <mestre.frederico at gmail.com> wrote:> Hello: > > > > I have a list of data frames, built like this: the second df is a result of > a function applied to the first, and so on. > > > > So the ith df is always dependent on the (i-1)th df. I've been doing this > using for loops. However I think I have too many for loops which is making > my code run slowly. > > > > Is there any workaround this? How can I avoid the use of for loops? > > > > As an example: > > > > output.list <- as.list(rep("", 100))#creation of a list > > > > output.list[[1]] <- df1#first position > > > > > > for(I in 2:100){#following positions > > > > df0 <- output.list[[i-1]] > > > > df0_1 <- f1(df0)#function applied to the previous df > > > > output.list[[i]] <- df0_1#new df > > > > } > > > > thanks, > > > > Frederico > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
You might gain some speed by not creating df0 and df0_1: for (i in 2:100) output.list[[i]] <- f1(output.list[[i-1]]) You can also look at the structure of the data frames. For example, if some of the elements in the data frames are factors but don't need to be factors, you might gain by preventing them from being factors. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 6/27/13 1:19 AM, "Frederico Mestre" <mestre.frederico at gmail.com> wrote:>Hello: > > > >I have a list of data frames, built like this: the second df is a result >of >a function applied to the first, and so on. > > > >So the ith df is always dependent on the (i-1)th df. I've been doing this >using for loops. However I think I have too many for loops which is making >my code run slowly. > > > >Is there any workaround this? How can I avoid the use of for loops? > > > >As an example: > > > >output.list <- as.list(rep("", 100))#creation of a list > > > >output.list[[1]] <- df1#first position > > > > > >for(I in 2:100){#following positions > > > >df0 <- output.list[[i-1]] > > > >df0_1 <- f1(df0)#function applied to the previous df > > > >output.list[[i]] <- df0_1#new df > > > >} > > > >thanks, > > > >Frederico > > > > > > > [[alternative HTML version deleted]] > >______________________________________________ >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.