diff --git a/NEWS.md b/NEWS.md index 5d523418b6..9e8c5a76c1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -134,6 +134,8 @@ 20. `c`, `seq` and `mean` of `ITime` objects now retain the `ITime` class via new `ITime` methods, [#3628](https://github.com/Rdatatable/data.table/issues/3628). Thanks @UweBlock for reporting. The `cut` and `split` methods for `ITime` have been removed since the default methods work, [#3630](https://github.com/Rdatatable/data.table/pull/3630). +20. `as.data.table.array` now handles the case when some of the array's dimension names are `NULL`, [#3636](https://github.com/Rdatatable/data.table/issues/3636). + #### 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. diff --git a/R/as.data.table.R b/R/as.data.table.R index 535e6f7ec6..8b87a9bc2e 100644 --- a/R/as.data.table.R +++ b/R/as.data.table.R @@ -90,10 +90,15 @@ as.data.table.array = function(x, keep.rownames=FALSE, key=NULL, sorted=TRUE, va if (!missing(sorted) && !is.null(key)) stop("Please provide either 'key' or 'sorted', but not both.") - dnx = dimnames(x) # NULL dimnames will create integer keys, not character as in table method - val = rev(if (is.null(dnx)) lapply(dx, seq.int) else dnx) + val = if (is.null(dnx)) { + lapply(dx, seq.int) + } else if (any(nulldnx<-sapply(dnx, is.null))) { + dnx[nulldnx] = lapply(dx[nulldnx], seq.int) #3636 + dnx + } else dnx + val = rev(val) if (is.null(names(val)) || all(!nzchar(names(val)))) setattr(val, 'names', paste0("V", rev(seq_along(val)))) if (value.name %chin% names(val)) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index bfadf57971..2a3fbe0791 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -14982,6 +14982,16 @@ test(2055.1, seq(from = as.ITime('00:00:00'), to = as.ITime('00:00:05'), by = 5L test(2055.2, c(as.ITime(0L), as.ITime(1L)), as.ITime(c(0L, 1L))) test(2055.3, mean(as.ITime(c(0L, 0L))), as.ITime(0L)) +# as.data.table.array some of dimnames are NULL, #3636 +a = array(1:8, dim=c(2L,2L,2L), dimnames=list(NULL, NULL, as.character(1:2))) +ans = data.table(V1=c(1L,1L,1L,1L,2L,2L,2L,2L), + V2=c(1L,1L,2L,2L,1L,1L,2L,2L), + V3=c("1","2","1","2","1","2","1","2"), + value=c(1L,5L,3L,7L,2L,6L,4L,8L), + key=c("V1","V2","V3")) +test(2056, as.data.table(a), ans) + + ################################### # Add new tests above this line # ###################################