ginan at metu.edu.tr
2013-Aug-23 20:33 UTC
[R] Replace two nested loop with an apply kind of function
Dear R users, I am confused with the usage of apply kind of functions instead of nested loops. Let me illustrate my problem, I have an array,named C, with dimesions c(nr,nr,nt*n). I want to fill in a Tmat array according to the rule as given below: Tmat<-array(diag(nt*nr), c(nt*nr,nt*nr,n)) for( i in 1:n) { for( t in 2:nt) { Tmat[(2*t-1):(2*t),(2*t-3):(2*t-2),i]<-C[,,(i*t)] } } Instead of using two for loops, I want to use any apply kind of function, but I couldn't figure it out. At this point, any help will be appreciated. Thank you very much for your interest. gul
Mark Sharp
2013-Aug-24 08:31 UTC
[R] Replace two nested loop with an apply kind of function
On Aug 23, 2013, at 3:33 PM, ginan@metu.edu.tr<mailto:ginan@metu.edu.tr> wrote: Dear R users, I am confused with the usage of apply kind of functions instead of nested loops. Let me illustrate my problem, I have an array,named C, with dimesions c(nr,nr,nt*n). I want to fill in a Tmat array according to the rule as given below: Tmat<-array(diag(nt*nr), c(nt*nr,nt*nr,n)) for( i in 1:n) { for( t in 2:nt) { Tmat[(2*t-1):(2*t),(2*t-3):(2*t-2),i]<-C[,,(i*t)] } } Instead of using two for loops, I want to use any apply kind of function, but I couldn't figure it out. At this point, any help will be appreciated. Thank you very much for your interest. gul ______________________________________________ R-help@r-project.org<mailto:R-help@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. [[alternative HTML version deleted]]
Mark Sharp
2013-Aug-24 08:35 UTC
[R] Replace two nested loop with an apply kind of function
The only reason to use one of the apply family of functions is because the idiom makes your code more clear. I do not see how that would be the case here. The apply functions are not typically any faster. If you really need speed the Rcpp package is very easy to use. Mark On Aug 23, 2013, at 3:33 PM, ginan at metu.edu.tr wrote:> > > Dear R users, > > I am confused with the usage of apply kind of functions instead of nested > loops. Let me illustrate my problem, I have an array,named C, with > dimesions c(nr,nr,nt*n). I want to fill in a Tmat array according to the > rule as given below: > > Tmat<-array(diag(nt*nr), c(nt*nr,nt*nr,n)) > > for( i in 1:n) { > for( t in 2:nt) { > Tmat[(2*t-1):(2*t),(2*t-3):(2*t-2),i]<-C[,,(i*t)] > } > } > > Instead of using two for loops, I want to use any apply kind of function, > but I couldn't figure it out. At this point, any help will be appreciated. > Thank you very much for your interest. > > > gul > > ______________________________________________ > 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.