Skip to content

Commit b6d6073

Browse files
frollapply correctly catches negative n (#7293)
* all * extend news * mark experimental * codecov * also test for negative n * test number * trailing ws * unit tests * unit tests, after save file * Apply suggestions from code review Co-authored-by: Michael Chirico <[email protected]> * frolladapt nicer news example * fix missed conflict in merge --------- Co-authored-by: Michael Chirico <[email protected]>
1 parent 9b421c8 commit b6d6073

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

R/frollapply.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ all_data.frame = function(x) all(vapply_1b(x, is.data.frame, use.names=FALSE))
6161
all_list = function(x) all(vapply_1b(x, is.list, use.names=FALSE))
6262
equal.lengths = function(x) length(unique(lengths(x))) <= 1L
6363
equal.nrows = function(x) length(unique(vapply(x, nrow, 0L))) <= 1L
64+
anyNAneg = function(x) anyNA(x) || any(x < 0L)
6465

6566
frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right","left","center"), adaptive=FALSE, partial=FALSE, give.names=FALSE, simplify=TRUE, x, n) {
6667
if (!missing(x)) {
@@ -147,7 +148,7 @@ frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right","
147148
nnam = names(N) ## used for give.names
148149
if (!is.integer(N))
149150
N = as.integer(N)
150-
if (anyNA(N))
151+
if (anyNAneg(N))
151152
stopf("'N' must be non-negative integer values (>= 0)")
152153
nn = length(N) ## top level loop for vectorized n
153154
} else {
@@ -158,7 +159,7 @@ frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right","
158159
stopf("length of integer vector(s) provided as list to 'N' argument must be equal to number of observations provided in 'X'")
159160
if (!is.integer(N))
160161
N = as.integer(N)
161-
if (anyNA(N))
162+
if (anyNAneg(N))
162163
stopf("'N' must be non-negative integer values (>= 0)")
163164
nn = 1L
164165
N = list(N)
@@ -172,7 +173,7 @@ frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right","
172173
stopf("'N' must be an integer vector or list of integer vectors")
173174
if (!all(vapply_1b(N, is.integer, use.names=FALSE)))
174175
N = lapply(N, as.integer)
175-
if (any(vapply_1b(N, anyNA, use.names=FALSE)))
176+
if (any(vapply_1b(N, anyNAneg, use.names=FALSE)))
176177
stopf("'N' must be non-negative integer values (>= 0)")
177178
nn = length(N)
178179
nnam = names(N)

inst/tests/froll.Rraw

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ test(6000.502, frollmax(c(5,NaN,1), 1L), c(5,NaN,1))
10051005
test(6000.503, frollmax(c(5,1,1,NaN,1,1,1), 2L), c(NA,5,1,NaN,NaN,1,1))
10061006
test(6000.504, frollmax(c(5,1,NA,NaN,1,1,1), 2L), c(NA,5,NA,NA,NaN,1,1))
10071007

1008-
# n==NA
1008+
# n==NA, n<0
10091009
test(6000.550, frollmean(1:3, NA), error="'n' must be an integer")
10101010
test(6000.551, frollmean(1:3, NA_integer_), error="'n' must be non-negative integer values (>= 0)")
10111011
test(6000.552, frollmean(1:3, NA, algo="exact"), error="'n' must be an integer")
@@ -1016,6 +1016,11 @@ test(6000.556, frollapply(FUN=mean, 1:3, NA), error="'N' must be an integer")
10161016
test(6000.557, frollapply(FUN=mean, 1:3, NA_integer_), error="'N' must be non-negative integer values (>= 0)")
10171017
test(6000.558, frollapply(FUN=mean, adaptive=TRUE, 1:3, c(2,NA,2)), error="'N' must be non-negative integer values (>= 0)")
10181018
test(6000.559, frollapply(FUN=mean, adaptive=TRUE, 1:3, list(c(2,NA,2))), error="'N' must be non-negative integer values (>= 0)")
1019+
test(6000.560, frollmean(1:3, -1), error="'n' must be non-negative integer values (>= 0)")
1020+
test(6000.561, frollmean(1:3, -1, algo="exact"), error="'n' must be non-negative integer values (>= 0)")
1021+
test(6000.562, frollapply(FUN=mean, 1:3, -1), error="'N' must be non-negative integer values (>= 0)")
1022+
test(6000.563, frollapply(FUN=mean, 1:3, c(0,-1,1), adaptive=TRUE), error="'N' must be non-negative integer values (>= 0)")
1023+
test(6000.564, frollapply(FUN=mean, 1:3, list(c(0,-1,1)), adaptive=TRUE), error="'N' must be non-negative integer values (>= 0)")
10191024

10201025
# n==0, k==0, k[i]==0
10211026
test(6001.111, frollmean(1:3, 0), c(NaN,NaN,NaN), options=c("datatable.verbose"=TRUE), output="window width of size 0")

0 commit comments

Comments
 (0)