Timeline for Mean of a sliding window in R
Current License: CC BY-SA 2.5
7 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jul 10, 2014 at 0:54 | comment | added | Jota |
Changing [i:(i+2)] to [i:(i+win.size-1)] would make the code more general, I think.
|
|
Sep 25, 2010 at 14:43 | comment | added | Matt Parker |
ucfagls, thanks for adding some info on pre-allocating when you don't know the ultimate size of the object - that's helpful. And while I agree that rollapply is probably almost definitely the way to go, I a) wanted to provide an example that would calculate the edge cases with less than three values, and b) just wanted to use while() , which I've yet to have a use for ;)
|
|
Sep 25, 2010 at 9:45 | comment | added | Gavin Simpson |
@mbq; Speed of results, whilst important, isn't the only consideration. Instead of having to reinvent the while and handle all the indexes etc in the custom solutions, the one-linear that is rollapply is much easier to understand and grep the intention of. Also, rollapply is likely to have had many more eyeballs checking its code than something I might cook up one afternoon. Horses for courses.
|
|
Sep 25, 2010 at 9:43 | comment | added | Gavin Simpson | wasn't me either, but as mentioned by yourself, pre-allocation of the result object will help with the speed issue. One trick, if you don't know, or it is tedious/difficult to determine, the size of the result object you need. Allocate something reasonable, perhaps pre-filling with NA. Then fill in with your loop, but add a check that if you are approaching the limit of the preallocated object, allocate another big chunk, and continue filling. | |
Sep 25, 2010 at 8:06 | comment | added | user88 |
It wasn't me, but this is slow (but not much slower than rollapply ).
|
|
Sep 24, 2010 at 19:59 | comment | added | Matt Parker | Please don't downvote without providing a comment. How am I supposed to know what's wrong? | |
Sep 24, 2010 at 16:40 | history | answered | Matt Parker | CC BY-SA 2.5 |