Skip to content

frollmean partial argument #4968

Description

@thatchersj

I'm not sure if this is a feature or a bug, however I was surprised by the behaviour of frollmean, which returns NA for the first n-1 values, even if fill = NA and na.rm = TRUE. This means that you get different results from manually pre-padding your data with NA. 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.

> 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

> 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
> frollmean(1:10, 3, na.rm = TRUE)
 [1] NA NA  2  3  4  5  6  7  8  9

> frollmean(c(NA, NA, 1:10), 3, na.rm = TRUE)[-(1:2)]
 [1] 1.0 1.5 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0

Session Info

> sessionInfo()

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] data.table_1.14.0

loaded via a namespace (and not attached):
[1] compiler_4.0.3

Activity

  1. self-assigned this
    on Apr 27, 2021
  2. jangorecki commented on Apr 28, 2021

    @jangorecki
    Member

    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 size
    

    I'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=TRUE controls 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

  3. thatchersj commented on Apr 28, 2021

    @thatchersj
    Author

    Hi Jan,
    Thank you very much for your thorough reply.

    ## "?" sign is not an NA here but a non existing value due to window size

    This is really the key to my query: that "missing" data beyond the limits of the dataset is different to missing data (NA values) within it, and na.rm does 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 zoo package I had not appreciated this before. Perhaps this could be made more explicit, for example:

    • partial window feature is not supported, although it can be accomplished by using adaptive=TRUE, see examples. NA is always returned for incomplete windows.
  4. added a commit that references this issue on Apr 29, 2021
  5. added a commit that references this issue on Apr 30, 2021
  6. jangorecki commented on Aug 29, 2022

    @jangorecki
    Member

    partial argument has been implemented in #5441

  7. changed the title [-]frollmean returns NA for the first n-1 values with na.rm = TRUE[/-] [+]frollmean partial argument[/+] on Sep 12, 2022
  8. added this to the 1.19.0 milestone on Jul 9, 2025
  9. modified the milestones: 1.19.0, 1.18.0 on Aug 27, 2025
  10. mcol commented on Aug 29, 2025

    @mcol
    Contributor

    Fixed by #7264.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions