diff --git a/NEWS.md b/NEWS.md index 7cf63f0b7..d781f90e1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -40,6 +40,8 @@ 5. Non-equi joins combining an equality condition with two inequality conditions on the same column (e.g., `on = .(id == id, val >= lo, val <= hi)`) no longer error, [#7641](https://github.com/Rdatatable/data.table/issues/7641). The internal `chmatchdup` remapping of duplicate `rightcols` was overwriting the original column indices, causing downstream code to reference non-existent columns. Thanks @tarun-t for the report and fix, and @aitap for the diagnosis. +6. `frollapply()` could have produce output longer than the input when the window-length is also longer than the input [#7646](https://github.com/Rdatatable/data.table/issues/7646). Thanks to @hadley-johnson for reporting and @jangorecki for the fix. + ### Notes 1. {data.table} now depends on R 3.5.0 (2018). diff --git a/R/frollapply.R b/R/frollapply.R index 9c1b0c26d..6a138547d 100644 --- a/R/frollapply.R +++ b/R/frollapply.R @@ -278,8 +278,11 @@ frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right"," cpy = copy ansMask = function(len, n) { mask = rep(TRUE, len) - if (n) ## handle n==0 + if (n) { ## handle n==0 + if (n > len) + n = len + 1L ## bugfix #7646 mask[seq_len(n-1L)] = FALSE + } mask } tight0 = function(i, dest, src, n) FUN(dest, ...) ## skip memcpy when n==0 diff --git a/inst/tests/froll.Rraw b/inst/tests/froll.Rraw index 5f8225910..22fd88423 100644 --- a/inst/tests/froll.Rraw +++ b/inst/tests/froll.Rraw @@ -2317,3 +2317,6 @@ test(6015.907, frolladapt(c(1L,3L,2L), 2L), error="be sorted, have no duplicates test(6015.908, frolladapt(c(1L,2L,2L), 2L), error="be sorted, have no duplicates, have no NAs") test(6015.909, frolladapt(c(1L,2L,NA_integer_), 2L), error="be sorted, have no duplicates, have no NAs") ## loop that checks for sorted will detect NAs as well, except for first element test(6015.910, frolladapt(c(NA_integer_,1L,2L), 2L), error="be sorted, have no duplicates, have no NAs") ## first NA is detected by extra check + +# frollapply can produce output longer than the input when the window-length is also longer than the input #7646 +test(6100.1, frollapply(c(1,2,3,4,5),N=7,FUN=sum), rep(NA, 5L))