# Error in data.table(character(0), NULL) : column or argument 2 is NULL
It would arguably be nice for data.table() to allow the construction of a table like this.
as.data.table.list allows it with no error:
DT = as.data.table(list(y = character(0), x = NULL))
NULL is idiomatic as a column value for grouped operations and (mostly) rbindlist:
data.table(g = 1:2)[, if (.GRP == 1L) Sys.Date(), by=g]
rbind(DT, list(y = "a", x = 12))
# also works, albeit with a warning
data.table(g = 1:2)[, .(v = c("yo", "zo"), z = if (.GRP == 1L) Sys.Date()), by=g]
# fails, with an informative error message
rbind(DT, list(y = "a", x = Sys.Date()))
And finally, the "wildcard" behavior of NULL in grouped operations and rbindlist is built upon in packages like vetr (see the "alike" vignette):
alike(DT, data.table(y = "a", x = "b")) # TRUE
alike(DT, data.table(y = "a", x = 12)) # TRUE
I may be overlooking some arguments against it. The main ones that come to mind are:
There is no way to add such a NULL column by reference.
Does this have any meaning or use for tables with rows?
There are bugs (like the one I linked at the top) that go away if NULL columns aren't a thing (?).
If it's decided that data.tables shouldn't have NULL columns (which would also be fine), then I guess as.data.table.list(x) may need to change to throw an error; or to drop the NULL elements of x even when max(lengths(x)) == 0.
The text was updated successfully, but these errors were encountered:
Yep: data.table's should not have NULL columns. Every column needs to have a type.
Have added some catches and tests. The things you mention should still work like the grouping example (I checked that's tested). If not, please raise a new issue.