Skip to content

Commit

Permalink
Closes #3785: rbindlist() now returns correct idcols for lists with d…
Browse files Browse the repository at this point in the history
…ifferent length vectors
  • Loading branch information
shrektan committed Aug 21, 2019
1 parent a8e926a commit 1b3c24c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@
27. `integer64` sum-by-group is now properly optimized, [#1647](https://github.com/Rdatatable/data.table/issues/1647), [#3464](https://github.com/Rdatatable/data.table/issues/3464). Thanks to @mlandry22-h2o for the report.
28. `rbindlist` now returns correct idcols for lists with different length vectors, [#3785](https://github.com/Rdatatable/data.table/issues/3785), [#3786](https://github.com/Rdatatable/data.table/pull/3786). Thanks to @shrektan for the report and fix.
#### NOTES
1. `rbindlist`'s `use.names="check"` now emits its message for automatic column names (`"V[0-9]+"`) too, [#3484](https://github.com/Rdatatable/data.table/pull/3484). See news item 5 of v1.12.2 below.
Expand Down
8 changes: 8 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -15671,6 +15671,14 @@ if (test_bit64) {
test(2077.06, int64_int32_match(d[, sum(i32, na.rm=TRUE), g], d[, sum(i64, na.rm=TRUE), g]))
}

# correct rbindlist() idcol for list elements with different length #3785
x = 1:1000
X = rbindlist(lapply(x, function(.) list(., 1:2, 2:3)), idcol = 'TAG')
test(2078.01, X$TAG, rep(x, each = 2))

x = setNames(x, paste0("nm_", x))
X = rbindlist(lapply(x, function(.) list(., 1:2, 2:3)), idcol = 'TAG')
test(2078.01, X$TAG, rep(names(x), each = 2))

###################################
# Add new tests above this line #
Expand Down
4 changes: 2 additions & 2 deletions src/rbindlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
for (int i=0,ansloc=0; i<LENGTH(l); ++i) {
SEXP li = VECTOR_ELT(l, i);
if (!length(li)) continue;
const int thisnrow = length(VECTOR_ELT(li, 0));
const int thisnrow = eachMax[i];
SEXP thisname = STRING_ELT(listNames, i);
for (int k=0; k<thisnrow; ++k) SET_STRING_ELT(idval, ansloc++, thisname);
}
Expand All @@ -261,7 +261,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
for (int i=0,ansloc=0; i<LENGTH(l); ++i) {
SEXP li = VECTOR_ELT(l, i);
if (!length(li)) continue;
const int thisnrow = length(VECTOR_ELT(li, 0));
const int thisnrow = eachMax[i];
for (int k=0; k<thisnrow; ++k) idvald[ansloc++] = i+1;
}
}
Expand Down

0 comments on commit 1b3c24c

Please sign in to comment.