Skip to content

Commit 8b1e3f9

Browse files
Don't use auto-inferred name in as.data.table.matrix under new rule (#7149)
1 parent 4cf9a70 commit 8b1e3f9

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
7171
13. In rare cases, `data.table` failed to expand ALTREP columns when assigning a full column by reference. This could result in the target column getting modified unintentionally if the next call to the data.table was a modification by reference of the source column. E.g. in `DT[, b := as.character(a)]` the string conversion gets deferred and subsequent modification of column `a` would also modify column `b`, [#5400](https://github.com/Rdatatable/data.table/issues/5400). Thanks to @aquasync for the report and Václav Tlapák for the PR.
7272
73-
14. `data.table()` function is now more aligned with `data.frame()` with respect to the names of the output when one of its inputs is a single-column matrix object, [#4124](https://github.com/Rdatatable/data.table/issues/4124). Thanks @PavoDive for the report and @jangorecki for the PR.
73+
14. `data.table()` function is now more aligned with `data.frame()` with respect to the names of the output when one of its inputs is a single-column matrix object, [#4124](https://github.com/Rdatatable/data.table/issues/4124). Thanks @PavoDive for the report, @jangorecki for the PR, and @MichaelChirico for a follow-up for back-compatibility.
7474
7575
15. Including an `ITime` object as a named input to `data.frame()` respects the provided name, i.e. `data.frame(a = as.ITime(...))` will have column `a`, [#4673](https://github.com/Rdatatable/data.table/issues/4673). Thanks @shrektan for the report and @MichaelChirico for the fix.
7676

R/as.data.table.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ as.data.table.matrix = function(x, keep.rownames=FALSE, key=NULL, ...) {
4848
if (!identical(keep.rownames, FALSE)) {
4949
# can specify col name to keep.rownames, #575
5050
ans = data.table(rn=rownames(x), x, keep.rownames=FALSE)
51+
# auto-inferred name 'x' is not back-compatible & inconsistent, #7145
52+
if (ncol(x) == 1L && is.null(colnames(x)))
53+
setnames(ans, 'x', 'V1')
5154
if (is.character(keep.rownames))
5255
setnames(ans, 'rn', keep.rownames[1L])
5356
return(ans)

inst/tests/tests.Rraw

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21276,13 +21276,46 @@ if (test_R.utils) local({
2127621276
})
2127721277

2127821278
# Create a data.table when one vector is transposed doesn't respect the name defined by user #4124
21279-
test(2321.1, DT <- data.table(a=1:2, b=matrix(1:2)), data.table(a=1:2, b=1:2))
21280-
test(2321.2, names(DT), names(data.frame(a=1:2, b=matrix(1:2))))
21281-
test(2321.3, DT <- data.table(a=integer(), b=matrix(1L, nrow=0L, ncol=1L)), data.table(a=integer(), b=integer()))
21282-
test(2321.4, names(DT), names(data.frame(a=integer(), b=matrix(1L, nrow=0L, ncol=1L))))
21279+
test(2321.01, DT <- data.table(a=1:2, b=matrix(1:2)), data.table(a=1:2, b=1:2))
21280+
test(2321.02, names(DT), names(data.frame(a=1:2, b=matrix(1:2))))
21281+
test(2321.03, DT <- data.table(a=integer(), b=matrix(1L, nrow=0L, ncol=1L)), data.table(a=integer(), b=integer()))
21282+
test(2321.04, names(DT), names(data.frame(a=integer(), b=matrix(1L, nrow=0L, ncol=1L))))
2128321283
## but respect named column vectors
21284-
test(2321.5, DT <- data.table(a=1:2, cbind(b=3:4)), data.table(a=1:2, b=3:4))
21285-
test(2321.6, names(DT), names(data.frame(a=1:2, cbind(b=3:4))))
21284+
test(2321.05, DT <- data.table(a=1:2, cbind(b=3:4)), data.table(a=1:2, b=3:4))
21285+
test(2321.06, names(DT), names(data.frame(a=1:2, cbind(b=3:4))))
21286+
## also respect old naming pattern when invoked indirectly, #7145
21287+
M = cbind(1:3)
21288+
test(2321.07, as.data.table(M), data.table(V1=1:3))
21289+
rownames(M) = c('a', 'b', 'c')
21290+
test(2321.08, as.data.table(M), data.table(V1=1:3))
21291+
test(2321.09, as.data.table(M, keep.rownames='id'), data.table(id=c('a', 'b', 'c'), V1=1:3))
21292+
colnames(M) = 'zz'
21293+
test(2321.10, as.data.table(M), data.table(zz=1:3))
21294+
test(2321.11, as.data.table(M, keep.rownames='id'), data.table(id=c('a', 'b', 'c'), zz=1:3))
21295+
colnames(M) = 'x'
21296+
test(2321.12, as.data.table(M), data.table(x=1:3))
21297+
test(2321.13, as.data.table(M, keep.rownames='id'), data.table(id=c('a', 'b', 'c'), x=1:3))
21298+
M = cbind(M, y=4:6)
21299+
test(2321.14, as.data.table(M), data.table(x=1:3, y=4:6))
21300+
test(2321.15, as.data.table(M, keep.rownames='id'), data.table(id=c('a', 'b', 'c'), x=1:3, y=4:6))
21301+
colnames(M) = c('A', 'B')
21302+
test(2321.16, as.data.table(M), data.table(A=1:3, B=4:6))
21303+
test(2321.17, as.data.table(M, keep.rownames='id'), data.table(id=c('a', 'b', 'c'), A=1:3, B=4:6))
21304+
colnames(M) = NULL
21305+
test(2321.18, as.data.table(M), data.table(V1=1:3, V2=4:6))
21306+
test(2321.19, as.data.table(M, keep.rownames='id'), data.table(id=c('a', 'b', 'c'), V1=1:3, V2=4:6))
21307+
colnames(M) = c('x', '')
21308+
test(2321.20, as.data.table(M), data.table(x=1:3, V2=4:6))
21309+
test(2321.21, as.data.table(M, keep.rownames='id'), data.table(id=c('a', 'b', 'c'), x=1:3, V2=4:6))
21310+
colnames(M) = c('', 'x')
21311+
test(2321.22, as.data.table(M), data.table(V1=1:3, x=4:6))
21312+
test(2321.23, as.data.table(M, keep.rownames='id'), data.table(id=c('a', 'b', 'c'), V1=1:3, x=4:6))
21313+
colnames(M) = c('', '')
21314+
test(2321.24, as.data.table(M), data.table(V1=1:3, V2=4:6))
21315+
test(2321.25, as.data.table(M, keep.rownames='id'), data.table(id=c('a', 'b', 'c'), V1=1:3, V2=4:6))
21316+
colnames(M) = c('A', '')
21317+
test(2321.26, as.data.table(M), data.table(A=1:3, V2=4:6))
21318+
test(2321.27, as.data.table(M, keep.rownames='id'), data.table(id=c('a', 'b', 'c'), A=1:3, V2=4:6))
2128621319

2128721320
# New fctr() helper: like factor() but retaining order by default #4837
2128821321
test(2322.01, levels(fctr(c("b","a","c"))), c("b","a","c"))

0 commit comments

Comments
 (0)