Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rbindlist(..., fill=TRUE) fills missing list columns with NA instead of NULL #4199

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ unit = "s")

6. `all.equal(DT, y)` no longer errors when `y` is not a data.table, [#4042](https://github.com/Rdatatable/data.table/issues/4042). Thanks to @d-sci for reporting and the PR.

7. `rbind` and `rbindlist` with `fill=TRUE` now fill missing `list` columns with `NA` instead of `NULL`, [#4198](https://github.com/Rdatatable/data.table/issues/4198). Thanks to @sritchie73 for reporting and the PR.

## NOTES

1. `as.IDate`, `as.ITime`, `second`, `minute`, and `hour` now recognize UTC equivalents for speed: GMT, GMT-0, GMT+0, GMT0, Etc/GMT, and Etc/UTC, [#4116](https://github.com/Rdatatable/data.table/issues/4116).
Expand Down
8 changes: 7 additions & 1 deletion inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -14191,7 +14191,7 @@ test(2002.03, rbindlist( list(list(a=1L, b=2L, x=NULL), list(a=2L, b=NULL, x=NUL
warning="Column 3 ['x'] of item 1 is length 0. This (and 2 others like it) has been filled with NA (NULL for list columns) to make each item uniform.")
# tests from #1302
test(2002.04, rbindlist( list(list(a=1L,z=list()), list(a=2L, z=list("m"))) ),
data.table(a=1:2, z=list(NULL, "m")),
data.table(a=1:2, z=list(NA, "m")),
warning="Column 2 ['z'] of item 1 is length 0. This (and 0 others like it) has been filled with NA")
test(2002.05, rbindlist( list( list(a=1L, z=list("z")), list(a=2L, z=list(c("a","b"))) )),
data.table(a=1:2, z=list("z", c("a","b"))))
Expand Down Expand Up @@ -16770,6 +16770,12 @@ test(2132.2, fifelse(TRUE, 1, s2), error = "S4 class objects (except nanot
test(2132.3, fcase(TRUE, s1, FALSE, s2), error = "S4 class objects (except nanotime) are not supported. Please see https://github.com/Rdatatable/data.table/issues/4131.")
rm(s1, s2, class2132)

# rbindlist(..., fill = TRUE) should fill list columns with NA not NULL
A = data.table(c1=0L, c2=list(1:3))
B = data.table(c1=1:2)
DT = data.table(c1=0:2, c2=list(1:3, NA, NA))
test(2133, rbind(A,B,fill=TRUE), DT)


########################
# Add new tests here #
Expand Down
12 changes: 10 additions & 2 deletions src/rbindlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,15 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
int w = usenames ? colMap[i*ncol + j] : j;
SEXP thisCol;
if (w==-1 || !length(thisCol=VECTOR_ELT(li, w))) { // !length for zeroCol warning above; #1871
writeNA(target, ansloc, thisnrow); // writeNA is integer64 aware and writes INT64_MIN
if (TYPEOF(target)==VECSXP) { // Fill list columns with NA instead of NULL #4198
for (int r=0; r<thisnrow; ++r) {
SEXP thisElement = PROTECT(allocVector(LGLSXP, 1)); nprotect++;
writeNA(thisElement, 0, 1);
SET_VECTOR_ELT(target, ansloc+r, thisElement);
}
} else {
writeNA(target, ansloc, thisnrow); // writeNA is integer64 aware and writes INT64_MIN
}
} else {
if ((TYPEOF(target)==VECSXP || TYPEOF(target)==EXPRSXP) && TYPEOF(thisCol)!=TYPEOF(target)) {
// do an as.list() on the atomic column; #3528
Expand All @@ -528,6 +536,6 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
}
}
}
UNPROTECT(nprotect); // ans, coercedForFactor, thisCol
UNPROTECT(nprotect); // ans, coercedForFactor, thisCol, thisElement
return(ans);
}