From 5e993bdfaa0cd421b026649b1d3e7a7e5c039dec Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 16 Jan 2020 18:14:58 +0800 Subject: [PATCH 1/2] as.data.table(table(NULL)) returns null data.table --- NEWS.md | 2 ++ R/as.data.table.R | 2 ++ inst/tests/tests.Rraw | 2 ++ 3 files changed, 6 insertions(+) diff --git a/NEWS.md b/NEWS.md index 2d607e9bad..991e067e19 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. `as.data.table(table(NULL))` no longer errors, [#4179](https://github.com/Rdatatable/data.table/issues/4179). An empty `data.table` is returned. This is in contrast to `as.data.frame(table(NULL))`, which returns 0-row, 1-column table; the 0-column version works better with other `data.table` functions like `rbindlist` (the one-column version would necessitate `use.names=TRUE`). + ## 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). diff --git a/R/as.data.table.R b/R/as.data.table.R index 71cbc310b1..3b513e8ae8 100644 --- a/R/as.data.table.R +++ b/R/as.data.table.R @@ -33,6 +33,8 @@ as.data.table.Date = as.data.table.ITime = function(x, keep.rownames=FALSE, key= # as.data.table.table - FR #4848 as.data.table.table = function(x, keep.rownames=FALSE, key=NULL, ...) { + # prevent #4179 & just cut out here + if (is.null(x)) return(null.data.table()) # Fix for bug #5408 - order of columns are different when doing as.data.table(with(DT, table(x, y))) val = rev(dimnames(provideDimnames(x))) if (is.null(names(val)) || !any(nzchar(names(val)))) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index f6d17a0762..75ef0e0dce 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -16770,6 +16770,8 @@ 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) +# NULL table -> NULL data.table, #4179 +test(2133, as.data.table(table(NULL)), data.table(NULL)) ######################## # Add new tests here # From 25b460ef43e70441dd55895544ec0c986aa678f5 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 16 Jan 2020 18:20:52 +0800 Subject: [PATCH 2/2] correct test --- R/as.data.table.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/as.data.table.R b/R/as.data.table.R index 3b513e8ae8..6781be4118 100644 --- a/R/as.data.table.R +++ b/R/as.data.table.R @@ -34,7 +34,7 @@ as.data.table.Date = as.data.table.ITime = function(x, keep.rownames=FALSE, key= # as.data.table.table - FR #4848 as.data.table.table = function(x, keep.rownames=FALSE, key=NULL, ...) { # prevent #4179 & just cut out here - if (is.null(x)) return(null.data.table()) + if (any(dim(x) == 0L)) return(null.data.table()) # Fix for bug #5408 - order of columns are different when doing as.data.table(with(DT, table(x, y))) val = rev(dimnames(provideDimnames(x))) if (is.null(names(val)) || !any(nzchar(names(val))))