mauede at alice.it
2009-Apr-26 17:42 UTC
[R] eager to learn how to use "sapply", "lapply", ...
After a year my R programming style is still very "C like". I am still writing a lot of "for loops" and finding it difficult to recognize where, in place of loops, I could just do the same with one line of code, using "sapply", "lapply", or the like. On-line examples for such high level function do not help me. Even if, sooner or later, I am getting my R scripts to do what I expect, I would really like to shake my C programming style off. I am staring at my R script and thinking "how can I improve it ?" For instance, I have a lot of loops similar to the following one and wonder whether I can replace them with a proper call to a high level R function that does the same: Nstart <- Nfour/(2^Lev) + 1 Nfinish <- Nstart -1 + Nfour/(2^Lev) LengLev <- Nfinish - Nstart + 1 NW <- floor(LengLev*N/Nfour) if(NW > 0){ for(j in Nstart:(Nstart + NW -1)){ Dw <- abs(Y[j]) Rnorm <- Rnorm + Dw^2 } } Thank you very much for helping me get better. Maura tutti i telefonini TIM! [[alternative HTML version deleted]]
I think you can replace your 'for' loop with vectorized operations: if(NW > 0){ Rnorm <- Rnorm + sum(abs(Y[Nstart:(Nstart + NW - 1)]) ^ 2) } On Sun, Apr 26, 2009 at 1:42 PM, <mauede at alice.it> wrote:> After a year my R programming style is still very "C like". > I am still writing a lot of "for loops" and finding it difficult to recognize where, in place of loops, I could just do the > same with one line of code, using "sapply", "lapply", or the like. > On-line examples for such high level function do not help me. > Even if, sooner or later, I am getting my R scripts to do what I expect, I would really like to shake my C programming style off. > I am staring at my R script and thinking "how can I improve it ?" > For instance, I have a lot of loops similar to the following one and wonder whether I can replace them with a proper call to a high level R function that does the same: > > ? ?Nstart <- Nfour/(2^Lev) + 1 > ? ? Nfinish <- Nstart -1 + Nfour/(2^Lev) > ? ? LengLev <- Nfinish - Nstart + 1 > ? ? NW <- floor(LengLev*N/Nfour) > ? ? if(NW > 0){ > ? ? ? for(j in Nstart:(Nstart + NW -1)){ > ? ? ? ? ?Dw <- abs(Y[j]) > ? ? ? ? ?Rnorm <- Rnorm + Dw^2 > ? ? ? } > ? ? } > > > Thank you very much for helping me get better. > Maura > > > > > > tutti i telefonini TIM! > > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
hadley wickham
2009-Apr-26 22:53 UTC
[R] eager to learn how to use "sapply", "lapply", ...
Have a look at the plyr package and associated documentation - http://had.co.nz/plyr Hadley On Sun, Apr 26, 2009 at 12:42 PM, <mauede at alice.it> wrote:> After a year my R programming style is still very "C like". > I am still writing a lot of "for loops" and finding it difficult to recognize where, in place of loops, I could just do the > same with one line of code, using "sapply", "lapply", or the like. > On-line examples for such high level function do not help me. > Even if, sooner or later, I am getting my R scripts to do what I expect, I would really like to shake my C programming style off. > I am staring at my R script and thinking "how can I improve it ?" > For instance, I have a lot of loops similar to the following one and wonder whether I can replace them with a proper call to a high level R function that does the same: > > ? ?Nstart <- Nfour/(2^Lev) + 1 > ? ? Nfinish <- Nstart -1 + Nfour/(2^Lev) > ? ? LengLev <- Nfinish - Nstart + 1 > ? ? NW <- floor(LengLev*N/Nfour) > ? ? if(NW > 0){ > ? ? ? for(j in Nstart:(Nstart + NW -1)){ > ? ? ? ? ?Dw <- abs(Y[j]) > ? ? ? ? ?Rnorm <- Rnorm + Dw^2 > ? ? ? } > ? ? } > > > Thank you very much for helping me get better. > Maura > > > > > > tutti i telefonini TIM! > > > ? ? ? ?[[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. >-- http://had.co.nz/