Skip to content

Commit d2deeeb

Browse files
committed
data.table names handling for unnamed matrix
1 parent b1b1832 commit d2deeeb

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ unit = "s")
107107

108108
12. `rbindlist` no longer errors when coercing complex vectors to character vectors, [#4202](https://github.com/Rdatatable/data.table/issues/4202). Thanks to @sritchie73 for reporting and the PR.
109109

110+
13. `data.table` function is now more aligned to base R about names of the output when one of its input is a single column matrix object, [#4124](https://github.com/Rdatatable/data.table/issues/4124). Thanks to @PavoDive for reporting. For matrix column objects it will also ensure to return non-duplicated names, [#3193](https://github.com/Rdatatable/data.table/issues/3193).
111+
110112
## NOTES
111113

112114
0. Retrospective license change permission was sought from and granted by 4 contributors who were missed in [PR#2456](https://github.com/Rdatatable/data.table/pull/2456), [#4140](https://github.com/Rdatatable/data.table/pull/4140). We had used [GitHub's contributor page](https://github.com/Rdatatable/data.table/graphs/contributors) which omits 3 of these due to invalid email addresses, unlike GitLab's contributor page which includes the ids. The 4th omission was a PR to a script which should not have been excluded; a script is code too. We are sorry these contributors were not properly credited before. They have now been added to the contributors list as displayed on CRAN. All the contributors of code to data.table hold its copyright jointly; your contributions belong to you. You contributed to data.table when it had a particular license at that time, and you contributed on that basis. This is why in the last license change, all contributors of code were consulted and each had a veto.

R/as.data.table.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ as.data.table.list = function(x,
127127
n = length(x)
128128
eachnrow = integer(n) # vector of lengths of each column. may not be equal if silent repetition is required.
129129
eachncol = integer(n)
130-
missing.check.names = missing(check.names)
130+
missing.check.names = missing(check.names) || is.null(check.names) # is.null for #3193
131+
if (missing.check.names) check.names = FALSE
131132
origListNames = if (missing(.named)) names(x) else NULL # as.data.table called directly, not from inside data.table() which provides .named, #3854
132133
for (i in seq_len(n)) {
133134
xi = x[[i]]
@@ -138,7 +139,11 @@ as.data.table.list = function(x,
138139
xi = x[[i]] = as.POSIXct(xi)
139140
} else if (is.matrix(xi) || is.data.frame(xi)) {
140141
if (!is.data.table(xi)) {
141-
xi = x[[i]] = as.data.table(xi, keep.rownames=keep.rownames) # we will never allow a matrix to be a column; always unpack the columns
142+
if (is.matrix(xi) && NCOL(xi)<=1L) { # 1 column matrix naming #4124
143+
xi = x[[i]] = c(xi)
144+
} else {
145+
xi = x[[i]] = as.data.table(xi, keep.rownames=keep.rownames) # we will never allow a matrix to be a column; always unpack the columns
146+
}
142147
}
143148
# else avoid dispatching to as.data.table.data.table (which exists and copies)
144149
} else if (is.table(xi)) {

R/data.table.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ data.table = function(..., keep.rownames=FALSE, check.names=FALSE, key=NULL, str
5858
names(x) = nd$vnames
5959
if (length(x)==0L) return( null.data.table() )
6060
if (length(x)==1L && (is.null(x[[1L]]) || (is.list(x[[1L]]) && length(x[[1L]])==0L))) return( null.data.table() ) #48
61+
if (missing(check.names)) check.names = NULL ## inherit missingness #3193
6162
ans = as.data.table.list(x, keep.rownames=keep.rownames, check.names=check.names, .named=nd$.named) # see comments inside as.data.table.list re copies
6263
if (!is.null(key)) {
6364
if (!is.character(key)) stop("key argument of data.table() must be character")

inst/tests/tests.Rraw

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16846,3 +16846,13 @@ A = data.table(A=c(complex(real = 1:3, imaginary=c(0, -1, 1)), NaN))
1684616846
test(2138.3, rbind(A,B), data.table(A=c(as.character(A$A), B$A)))
1684716847
A = data.table(A=as.complex(rep(NA, 5)))
1684816848
test(2138.4, rbind(A,B), data.table(A=c(as.character(A$A), B$A)))
16849+
16850+
# Create a data.table when one vector is transposed doesn't respect the name defined by user #4124
16851+
test(2139.1, data.table(a=1:2, b=matrix(1:2)), data.table(a=1:2, b=1:2))
16852+
test(2139.2, names(data.table(a=1:2, b=matrix(1:2))), names(data.frame(a=1:2, b=matrix(1:2))))
16853+
test(2139.3, data.table(a=1L[0L], b=matrix(1L)[0L]), data.table(a=1L[0L], b=1L[0L])) # length 0
16854+
test(2139.4, names(data.table(a=1L[0L], b=matrix(1L)[0L])), names(data.frame(a=1L[0L], b=matrix(1L)[0L])))
16855+
# data.table(vector, matrix) can return duplicate column names #3193
16856+
is.unq = function(x) length(unique(x)) == length(x)
16857+
test(2139.5, is.unq(names(data.table(1:5, matrix(6:15, nrow = 5L)))))
16858+
test(2139.6, is.unq(names(data.table(1L[0L], matrix(6:15, nrow=5L)[0L]))))

0 commit comments

Comments
 (0)