Skip to content

Commit 131af20

Browse files
data.table names handling for unnamed matrix (#4363)
* data.table names handling for unnamed matrix * tweak NEWS * tighten up tests * walk back update for check.names default * retain column vector's name, if present * further shrink diff --------- Co-authored-by: Michael Chirico <[email protected]>
1 parent 94f4081 commit 131af20

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
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.
4848

49+
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.
50+
4951
### NOTES
5052

5153
1. Continued work to remove non-API C functions, [#6180](https://github.com/Rdatatable/data.table/issues/6180). Thanks Ivan Krylov for the PRs and for writing a clear and concise guide about the R API: https://aitap.codeberg.page/R-api/.

R/as.data.table.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ as.data.table.list = function(x,
142142
xi = x[[i]] = as.POSIXct(xi)
143143
} else if (is.matrix(xi) || is.data.frame(xi)) {
144144
if (!is.data.table(xi)) {
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
145+
if (is.matrix(xi) && NCOL(xi)<=1L && is.null(colnames(xi))) { # 1 column matrix naming #4124
146+
xi = x[[i]] = c(xi)
147+
} else {
148+
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
149+
}
146150
}
147151
# else avoid dispatching to as.data.table.data.table (which exists and copies)
148152
} else if (is.table(xi)) {

inst/tests/tests.Rraw

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21130,16 +21130,16 @@ class(DF) = c("tbl_df", "tbl", "data.frame")
2113021130
test(2309.02, key(as.data.table(DF, key="t")), "t")
2113121131

2113221132
# data.table keyed with "b"
21133-
DT = data.table(a = 1:5, b = 1:5, x = 1:5, key = "b")
21134-
test(2309.03, key(as.data.table(DT, key="a")), "a")
21135-
test(2309.04, key(as.data.table(DT)), "b")
21136-
test(2309.05, key(as.data.table(DT, key=NULL)), NULL)
21133+
DT = data.table(a = 1:5, b = 1:5, x = 1:5, key = "b")
21134+
test(2309.03, key(as.data.table(DT, key="a")), "a")
21135+
test(2309.04, key(as.data.table(DT)), "b")
21136+
test(2309.05, key(as.data.table(DT, key=NULL)), NULL)
2113721137

2113821138
# non-keyed data.table
2113921139
DT = data.table(a = 1:5, b = 1:5, x = 1:5)
21140-
test(2309.06, key(as.data.table(DT, key="a")), "a")
21141-
test(2309.07, key(as.data.table(DT)), NULL)
21142-
test(2309.08, key(as.data.table(DT, key=NULL)), NULL)
21140+
test(2309.06, key(as.data.table(DT, key="a")), "a")
21141+
test(2309.07, key(as.data.table(DT)), NULL)
21142+
test(2309.08, key(as.data.table(DT, key=NULL)), NULL)
2114321143

2114421144
# as.data.table(x, keep.rownames=TRUE) keeps rownames for class(x)==c("*", "data.frame")
2114521145
df = structure(list(i = 1:2), class = c("tbl", "data.frame"), row.names = c("a","b"))
@@ -21228,3 +21228,12 @@ if (test_R.utils) local({
2122821228
})
2122921229
test(2320.2, fread(tmp), error="R.utils::decompressFile failed to decompress", warning="invalid")
2123021230
})
21231+
21232+
# Create a data.table when one vector is transposed doesn't respect the name defined by user #4124
21233+
test(2321.1, DT <- data.table(a=1:2, b=matrix(1:2)), data.table(a=1:2, b=1:2))
21234+
test(2321.2, names(DT), names(data.frame(a=1:2, b=matrix(1:2))))
21235+
test(2321.3, DT <- data.table(a=integer(), b=matrix(1L, nrow=0L, ncol=1L)), data.table(a=integer(), b=integer()))
21236+
test(2321.4, names(DT), names(data.frame(a=integer(), b=matrix(1L, nrow=0L, ncol=1L))))
21237+
## but respect named column vectors
21238+
test(2321.5, DT <- data.table(a=1:2, cbind(b=3:4)), data.table(a=1:2, b=3:4))
21239+
test(2321.6, names(DT), names(data.frame(a=1:2, cbind(b=3:4))))

0 commit comments

Comments
 (0)