[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
>value > 15. 
>In my loop, I consider the first numeric value of my column as
>Then, I want to test the second value. If the absolute difference with
>previous correct one is <15, it's a new correct one, but if it's >15,
>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
>one goes under 15 (and so, this value is the new "correct" one, and so
>for the rest of the column).
>My loop is already doing the trick, but I just want to speed it up (or
>another faster way to do the job).
>Hope it's more understandable right now! 
>View this message in context:
>Sent from the R help mailing list archive at Nabble.com.
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list