Skip to content

Commit 7db13b9

Browse files
jangoreckiMichaelChiricoben-schwen
authored
Fix frollapply to handle n > len case (#7650)
* Fix frollapply to handle n > len case * unit test for bugfix * news entry * Update NEWS.md Co-authored-by: Michael Chirico <chiricom@google.com> * Apply suggestion from @ben-schwen Co-authored-by: Benjamin Schwendinger <52290390+ben-schwen@users.noreply.github.com> --------- Co-authored-by: Michael Chirico <chiricom@google.com> Co-authored-by: Benjamin Schwendinger <52290390+ben-schwen@users.noreply.github.com>
1 parent 9d81e6d commit 7db13b9

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
7. `fread()` can now read from connections directly by spilling to a temporary file first, [#561](https://github.com/Rdatatable/data.table/issues/561). For the best throughput, point `tmpdir=` (or the global temp directory) to fast storage like an SSD or RAM. Thanks to Chris Neff for the report and @ben-schwen for the implementation.
4848

49+
8. `frollapply()` no longer produces 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.
50+
4951
### Notes
5052

5153
1. {data.table} now depends on R 3.5.0 (2018).

R/frollapply.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,10 @@ frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right","
278278
cpy = copy
279279
ansMask = function(len, n) {
280280
mask = rep(TRUE, len)
281-
if (n) ## handle n==0
281+
if (n) { ## handle n==0
282+
n = min(n, len + 1L) # cap at len (bugfix #7646)
282283
mask[seq_len(n-1L)] = FALSE
284+
}
283285
mask
284286
}
285287
tight0 = function(i, dest, src, n) FUN(dest, ...) ## skip memcpy when n==0

inst/tests/froll.Rraw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,3 +2317,6 @@ test(6015.907, frolladapt(c(1L,3L,2L), 2L), error="be sorted, have no duplicates
23172317
test(6015.908, frolladapt(c(1L,2L,2L), 2L), error="be sorted, have no duplicates, have no NAs")
23182318
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
23192319
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
2320+
2321+
# frollapply can produce output longer than the input when the window-length is also longer than the input #7646
2322+
test(6100.1, frollapply(c(1,2,3,4,5),N=7,FUN=sum), rep(NA, 5L))

0 commit comments

Comments
 (0)