Skip to content

Commit 7e2e760

Browse files
committed
partial2adaptive does not wrap in a list when not needed
1 parent ba5926c commit 7e2e760

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

R/froll.R

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
trimn = function(n, len, align) {
33
n = min(n, len) ## so frollsum(1:2, 3, partial=TRUE) works
44
if (align=="right")
5-
c(seq.int(n), rep.int(n, len-n))
5+
c(seq_len(n), rep.int(n, len-n))
66
else
7-
c(rep.int(n, len-n), rev(seq.int(n)))
7+
c(rep.int(n, len-n), rev(seq_len(n)))
88
}
99
trimnadaptive = function(n, align) {
1010
if (align=="right")
@@ -25,9 +25,11 @@ trimnadaptive = function(n, align) {
2525
# frollsum(list(1:4, 2:5), 2:3, partial=FALSE, adaptive=FALSE)
2626
# frollsum(list(1:4, 2:5), 2:3, partial=TRUE, adaptive=FALSE)
2727
partial2adaptive = function(x, n, align, adaptive) {
28+
if (!length(n))
29+
stopf("n must be non 0 length")
2830
if (align=="center")
2931
stopf("'partial' cannot be used together with align='center'")
30-
if (is.list(x) && length(unique(lengths(x)))!=1L)
32+
if (is.list(x) && length(unique(lengths(x))) != 1L)
3133
stopf("'partial' does not support variable length of columns in 'x'")
3234
len = if (is.list(x)) length(x[[1L]]) else length(x)
3335
verbose = getOption("datatable.verbose")
@@ -38,9 +40,11 @@ partial2adaptive = function(x, n, align, adaptive) {
3840
stopf("n must be an integer vector or a list of integer vectors")
3941
if (verbose)
4042
catf("partial2adaptive: froll partial=TRUE trimming 'n' and redirecting to adaptive=TRUE\n")
41-
if (length(n)>1L) {
43+
if (length(n) > 1L) {
44+
## c(2,3) -> list(c(1,2,2,2),c(1,2,3,3)) ## for x=1:4
4245
lapply(n, len, align, FUN=trimn)
4346
} else {
47+
## 3 -> c(1,2,3,3) ## for x=1:4
4448
trimn(n, len, align)
4549
}
4650
} else {
@@ -54,7 +58,13 @@ partial2adaptive = function(x, n, align, adaptive) {
5458
stopf("length of vectors in 'x' must match to length of adaptive window in 'n'")
5559
if (verbose)
5660
catf("partial2adaptive: froll adaptive=TRUE and partial=TRUE trimming 'n'\n")
57-
lapply(n, align, FUN=trimnadaptive)
61+
if (is.numeric(n)) {
62+
## c(3,3,3,2) -> c(1,2,3,2) ## for x=1:4
63+
trimnadaptive(n, align)
64+
} else {
65+
## list(c(3,3,3,2),c(4,2,3,3)) -> list(c(1,2,3,2),c(1,2,3,3)) ## for x=1:4
66+
lapply(n, align, FUN = trimnadaptive)
67+
}
5868
}
5969
}
6070

@@ -63,8 +73,6 @@ froll = function(fun, x, n, fill=NA, algo, align=c("right","left","center"), na.
6373
if (isTRUE(give.names))
6474
orig = list(n=n, adaptive=adaptive)
6575
if (isTRUE(partial)) {
66-
if (!length(n))
67-
stopf("n must be non 0 length")
6876
n = partial2adaptive(x, n, align, adaptive)
6977
adaptive = TRUE
7078
}

0 commit comments

Comments
 (0)