Skip to content

Commit e2ce90a

Browse files
gshift works in subset queries (#5963)
* add fix for subset * add NEWS --------- Co-authored-by: Benjamin Schwendinger <[email protected]>
1 parent 815e054 commit e2ce90a

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

NEWS.md

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

2727
2. `dcast` handles coercion of `fill` to `integer64` correctly, [#4561](https://github.com/Rdatatable/data.table/issues/4561). Thanks to @emallickhossain for the bug report and @MichaelChirico for the fix.
2828

29+
3. Optimized `shift` per group produced wrong results when simultaneously subsetting, for example, `DT[i==1L, shift(x), by=group]`, [#5962](https://github.com/Rdatatable/data.table/issues/5962). Thanks to @renkun-ken for the report and Benjamin Schwendinger for the fix.
30+
2931
## NOTES
3032

3133
1. `transform` method for data.table sped up substantially when creating new columns on large tables. Thanks to @OfekShilon for the report and PR. The implemented solution was proposed by @ColeMiller1.

inst/tests/tests.Rraw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17841,6 +17841,9 @@ test(2224.3, DT[, shift(x), g], error="Type 'list' is not supported by GForce gs
1784117841
# single vector is un-list()'d, consistently with plain shift(), #5939
1784217842
DT = data.table(x=1, g=1)
1784317843
test(2224.4, DT[, .(shift(x)), g], DT[, shift(x), g])
17844+
# gshift with subset #5962
17845+
DT = data.table(x = 1:3, g = rep(1:2, each=3L))
17846+
test(2224.5, DT[g==1L, shift(x), by=g, verbose=TRUE], data.table(g=1L, V1=c(NA,1L,2L)), output="Making each group and running j (GForce TRUE)")
1784417847

1784517848
# groupingsets by named by argument
1784617849
test(2225.1, groupingsets(data.table(iris), j=sum(Sepal.Length), by=c('Sp'='Species'), sets=list('Species')),

src/gsumm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ SEXP gshift(SEXP x, SEXP nArg, SEXP fillArg, SEXP typeArg) {
12031203
bool lag;
12041204
const bool cycle = stype == CYCLIC;
12051205

1206-
R_xlen_t nx = xlength(x), nk = length(nArg);
1206+
R_xlen_t nk = length(nArg);
12071207
if (!isInteger(nArg)) error(_("Internal error: n must be integer")); // # nocov
12081208
const int *kd = INTEGER(nArg);
12091209
for (int i=0; i<nk; i++) if (kd[i]==NA_INTEGER) error(_("Item %d of n is NA"), i+1);
@@ -1220,7 +1220,7 @@ SEXP gshift(SEXP x, SEXP nArg, SEXP fillArg, SEXP typeArg) {
12201220
}
12211221
R_xlen_t ansi = 0;
12221222
SEXP tmp;
1223-
SET_VECTOR_ELT(ans, g, tmp=allocVector(TYPEOF(x), nx));
1223+
SET_VECTOR_ELT(ans, g, tmp=allocVector(TYPEOF(x), n));
12241224
#define SHIFT(CTYPE, RTYPE, ASSIGN) { \
12251225
const CTYPE *xd = (const CTYPE *)RTYPE(x); \
12261226
const CTYPE fill = RTYPE(thisfill)[0]; \

0 commit comments

Comments
 (0)