Skip to content

Commit c9f5507

Browse files
committed
top align
1 parent 4964908 commit c9f5507

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

R/last.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ last = function(x, n=1L, na.rm=FALSE, ...) {
5353
l = vapply_1i(ans, length)
5454
m = max(l)
5555
for (i in which(l<m)) { # pad with NA
56-
ans[[i]] = if (first) c(ans[[i]], rep(NA, m-l[i]))
57-
else c(rep(NA, m-l[i]), ans[[i]])
56+
ans[[i]] = c(ans[[i]], rep(NA, m-l[i]))
5857
}
5958
if (is.data.table(x)) setDT(ans) else setDF(ans)
6059
setattr(ans, "class", class(x))

inst/tests/tests.Rraw

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18883,7 +18883,7 @@ for (opt in c(0, Inf)) {
1888318883
test(test_no+.34, DT[, first(B,na.rm=TRUE,n=2), by=grp, verbose=TRUE], data.table(grp=INT(1,2,2,3,4), V1=c(1,pi,3,3,pi)), output=out)
1888418884
test(test_no+.35, DT[, last(D,na.rm=TRUE,n=1), by=grp, verbose=TRUE], data.table(grp=INT(1,2,4), V1=c(1i,3i,4i)), output=out)
1888518885
test(test_no+.41, DT[, last(.SD, na.rm=TRUE, n=2), by=grp, verbose=TRUE],
18886-
data.table(grp=INT(1,2,2,3,4,4), A=INT(1,NA,2,NA,3,3), B=c(1,pi,3,3,NA,pi), C=c("a",NA,"b","c",NA,"d"), D=c(1i,2i,3i,NA,NA,4i), E=list(NA,NA,c("a","b"),list(1:2),NA,3:4)),
18886+
data.table(grp=INT(1,2,2,3,4,4), A=INT(1,2,NA,NA,3,3), B=c(1,pi,3,3,pi,NA), C=c("a","b",NA,"c","d",NA), D=c(1i,2i,3i,NA,4i,NA), E=list(NA,c("a","b"),NA,list(1:2),3:4,NA)),
1888718887
output=out)
1888818888
test(test_no+.42, DT[, last(.SD, na.rm="row", n=2), by=grp, verbose=TRUE],
1888918889
data.table(grp=INT(1,1,2,2,3,4,4), A=c(NA,NA,NA,2L,NA,NA,3L), B=c(NA,NA,NA,3,NA,NA,pi), C=c(NA,NA,NA,"b",NA,NA,"d"), D=c(NA,NA,NA,3i,NA,NA,4i), E=list(NA,NA,NA,c("a","b"),NA,NA,3:4)),
@@ -18911,6 +18911,15 @@ for (opt in c(0, Inf)) {
1891118911
test(test_no+.72, DT[,first(A, na.rm=NA),by=grp,verbose=TRUE], error="na.rm", output=out)
1891218912
test(test_no+.73, last(DT$A, na.rm=NA), error="na.rm")
1891318913
test(test_no+.74, DT[,last(A, na.rm=NA),by=grp,verbose=TRUE], error="na.rm", output=out)
18914+
18915+
# aligning two gforce dynamic columns the same between optimized and unoptimized
18916+
# needs to be top aligned otherwise dogroups.c would need knowledge of R first/last (last was aligned at the bottom in the PR before this test added)
18917+
# but outstanding is that dogroups.c recycles length-1 whereas gforce_dynamic currently NA fills with vector output from
18918+
# first/last n>1 na.rm=TRUE in mind. So this test fails when not optimized (2240.81) currently.
18919+
test(test_no+.81, DT[, .(first(B, n=2, na.rm=TRUE), last(C, na.rm=TRUE)), by=grp, verbose=TRUE],
18920+
data.table(grp=INT(1,2,2,3,4), V1=c(1,pi,3,3,pi), V2=c("a","b",NA,"c","d")),
18921+
output=out)
18922+
1891418923
test_no = trunc(test_no)+1
1891518924
}
1891618925

src/gsumm.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,10 @@ SEXP gforce(SEXP env, SEXP jsub, SEXP o, SEXP f, SEXP l, SEXP irowsArg, SEXP grp
376376
const int thislen = ss ? ss[g] : grp_allocated; \
377377
const int targetlen = lensp[g]; \
378378
const int napad = targetlen-thislen; \
379-
if (!first) { \
380-
val=RNA; for (int i=0; i<napad; ++i) ASSIGN; \
381-
k += grp_allocated-thislen; \
382-
} \
383-
for (int i=0; i<thislen; ++i) { val=xd[k++]; ASSIGN; } \
384-
if (first) { \
385-
val=RNA; for (int i=0; i<napad; ++i) ASSIGN; \
386-
k += grp_allocated-thislen; \
387-
} \
379+
k += (!first)*(grp_allocated-thislen); \
380+
for (int i=0; i<thislen; ++i) { val=xd[k++]; ASSIGN; } \
381+
val=RNA; for (int i=0; i<napad; ++i) ASSIGN; \
382+
k += (first)*(grp_allocated-thislen); \
388383
} \
389384
ansd++; /* just to suppress unused-variable warning in STRSXP and VECSXP cases */ \
390385
} break;

0 commit comments

Comments
 (0)