Repository navigation
frollmean partial argument #4968
Description
Activity
Hi,
Below I presented how computation works for first two windows in both presented cases:> frollmean(1:10, 2, na.rm = TRUE) [1] NA 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 w1: mean(c(?,1)) = NA w2: mean(c(1,2)) = 1.5 > frollmean(c(NA, 1:10), 2, na.rm = TRUE)[-1] [1] 1.0 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 w1: mean(c(?,NA)) = NA w2: mean(c(NA,1)) = 1 ## "mean" assumes na.rm=TRUE ## "?" sign is not an NA here but a non existing value due to window sizeI'm not sure if this is a feature or a bug
It is a feature. Rolling statistics are doing reduction only within each individual window, and not at the whole answer.
returns NA for the first n-1 values, even if fill = NA and na.rm = TRUE
Number of elements on the output is always equal to number of elements in the input.
na.rm=TRUEcontrols how NAs in the input are propagated. If window is to short to be computed it is unknown.It would be good to get others' thoughts on this behaviour
You are welcome to propose improvements to documentation. In "note" section it already mentions
rolling function will always return result of the same length as input.
https://rdatatable.gitlab.io/data.table/library/data.table/html/froll.html
Reacted by thatchersjHi Jan,
Thank you very much for your thorough reply.## "?" sign is not an NA here but a non existing value due to window sizeThis is really the key to my query: that "missing" data beyond the limits of the dataset is different to missing data (
NAvalues) within it, andna.rmdoes not, therefore, apply to it.Looking again at the documentation, I believe this is implication of the final note, though not being very familiar with the
zoopackage I had not appreciated this before. Perhaps this could be made more explicit, for example:partialwindow feature is not supported, although it can be accomplished by usingadaptive=TRUE, see examples.NAis always returned for incomplete windows.
Reacted by Jan Gorecki- added a commit that references this issue
on Apr 30, 2021 partialargument has been implemented in #5441- linked a pull request that will close this issuerolling functions: adaptive left, frollmax, frollapply adaptive, partial #5441
on Aug 29, 2022 - changed the title
[-]frollmean returns NA for the first n-1 values with na.rm = TRUE[/-][+]frollmean partial argument[/+]on Sep 12, 2022 - added a commit that references this issue
on May 8, 2025 Fixed by #7264.
Reacted by Jan Gorecki
I'm not sure if this is a feature or a bug, however I was surprised by the behaviour of
frollmean, which returnsNAfor the firstn-1values, even iffill = NAandna.rm = TRUE. This means that you get different results from manually pre-padding your data withNA. It would be good to get others' thoughts on this behaviour.Examples
I expected the two lines in the following examples to return the same values, but they do not.
Session Info