[R] How to speed up a double loop?

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue Mar 3 09:20:11 CET 2015


Usually there is some trick with diff or role or cumsum that can make something like this work, but all I can come up with is using Rcpp to brute force it. That works best if you create a package, but it can work okay without it if this is a one-off analysis.

Note that demolishing data as in your example makes most statisticians uncomfortable.. particularly in the name of removing outliers. I would recommend creating a quality check column and then perhaps subsetting into a new data frame if you felt it was necessary.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On March 2, 2015 6:12:44 AM PST, jeff6868 <geoffrey_klein at etu.u-bourgogne.fr> wrote:
>Hi Petr,
>
>Thanks for your reply,
>
>Actually it's not what I'm looking for. The aim is not simply to remove
>each
>value > 15. 
>
>In my loop, I consider the first numeric value of my column as
>"correct".
>Then, I want to test the second value. If the absolute difference with
>the
>previous correct one is <15, it's a new correct one, but if it's >15,
>then
>it's a wrong one. 
>If it's a wrong one, it has to test the third one to check if it's
>still >15
>from the last correct value (first one).
>The value becomes correct again when the difference with the last
>correct
>one goes under 15 (and so, this value is the new "correct" one, and so
>one
>for the rest of the column).
>
>My loop is already doing the trick, but I just want to speed it up (or
>maybe
>another faster way to do the job).
>Hope it's more understandable right now! 
>
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/How-to-speed-up-a-double-loop-tp4704054p4704061.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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