Skip to content

Error message when (ab)using frollmean on list with adaptive = TRUE, and notes on documentation  #5306

Description

@Henrik-P

I have a list like

set.seed(1)
l = list(sample(5), sample(4))
l
# [[1]]
# [1] 1 4 3 5 2
# 
# [[2]]
# [1] 1 3 4 2

I want to apply a rolling mean with adaptive window size.

From ?frollmean:

x ... May also be a list, in which case the rolling function is applied to each of its elements

Thus, e.g. frollmean(x = l, n = 3) works just fine.

On n:

Adaptive rolling functions also accept a list of integer vectors.

If adaptive=TRUE, then n must be a list. Each list element must be integer vector of window sizes corresponding to every single observation in each column

Because I have a list, I interpret that "each column" could also mean "each list element".

So far everything seems fine and I am eager to try my code! Yes, additional relevant help text follows further down, but let's ignore it in order to demonstrate the error. So here we go, starting by creating a list of window sizes, where each list element is of the same length as the input list elements:

win_size = 3
win_sizes = lapply(lengths(l), function(len) c(seq.int(win_size), rep(win_size, len - win_size)))
win_sizes
# [[1]]
# [1] 1 2 3 3 3
#
# [[2]]
# [1] 1 2 3 3

frollmean(x = l, n = win_sizes, adaptive = TRUE)
# Error in froll(fun = "mean", x = x, n = n, fill = fill, algo = algo, align = align,  : 
#  length of integer vector(s) provided as list to 'n' argument must be equal to
# number of observations provided in 'x'

I may misinterpret the error message, but to me it suggests that the problem is "only" the n argument and that the number of values in each element in n ("win_sizes") differs from the number of values in each element in the list "l" passed to x. Which is not the case (lengths(l); lengths(win_sizes)). And the user may start to check and re-check the lengths.

However, as I wrote above, there is more to the help text:

Due to the logic of adaptive rolling functions, the following restrictions apply:
if list of vectors is passed to x, then all vectors within it must have equal length.

Thus, the main underlying issue seems to be the fact that the elements of the list passed to x do not have equal length, and not that n is specified incorrectly. If so, should the error message be adjusted accordingly?


Related notes on frollmean help text:

I'm a bit curious about the "Due to the logic of adaptive rolling functions" in

Due to the logic of adaptive rolling functions, the following restrictions apply:
if list of vectors is passed to x, then all vectors within it must have equal length.

This may come across like a more general constraint of adaptive rolling functions, which would make it impossible to apply them on lists with elements of different lengths. But I'm sure I have misinterpreted the text. Does it "only" refer to a limitation of the current implementation of frollmean? Perhaps this could be clarified.


Finally, from Details:

If adaptive=TRUE, then n must be a list.

This doesn't seem to be correct. At least in the first Example using adaptive=TRUE, a vector is passed to n. Furthermore, in Arguments the requirements on n seem less strict:

Adaptive rolling functions also accept a list of integer vectors. [not "must be a list"]

Activity

  1. jangorecki commented on Jan 12, 2022

    @jangorecki
    Member

    Thanks for the report. We should either correct the error message, or remove the constraint. I might have introduce it to simplify input validation. List of unequal lengths was not my use case, and I seen it as rather uncommon one, considering structure of data.table/frame.

    if (i > 0 && (inx[i]!=inx[i-1])) // variable length list input not allowed for adaptive roll
    this is related part.
    If you think it is common use case then we should try to lift this constraint, otherwise just correcting error should be fine.

  2. Henrik-P commented on Jan 13, 2022

    @Henrik-P
    Author

    I think an adjustment of the error message only is perfectly fine at this stage.

    Thank you so much Jan for your great work with the rolling functions!

  3. jangorecki commented on Aug 30, 2022

    @jangorecki
    Member

    As for the "due to logic" part is related to fact that each window size vector (when providing multiple adaptive window sizes in n) is applied to each column (or a vector in a list). So if columns has different length, then window widths in n will not match then. For adaptive it is required that window widths are matching in length to the length of input.
    Other your comments I believe has been addressed in linked PR. If anything is missing, please let feedback directly there.

  4. added a commit that references this issue on Sep 12, 2022
  5. added this to the 1.19.0 milestone on Jul 9, 2025
  6. modified the milestones: 1.19.0, 1.18.0 on Aug 27, 2025
  7. 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

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions