Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

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.

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.

### NOTES

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/.
Expand Down
6 changes: 5 additions & 1 deletion R/as.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ as.data.table.list = function(x,
xi = x[[i]] = as.POSIXct(xi)
} else if (is.matrix(xi) || is.data.frame(xi)) {
if (!is.data.table(xi)) {
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
if (is.matrix(xi) && NCOL(xi)<=1L && is.null(colnames(xi))) { # 1 column matrix naming #4124
xi = x[[i]] = c(xi)
} else {
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
}
}
# else avoid dispatching to as.data.table.data.table (which exists and copies)
} else if (is.table(xi)) {
Expand Down
23 changes: 16 additions & 7 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -21130,16 +21130,16 @@ class(DF) = c("tbl_df", "tbl", "data.frame")
test(2309.02, key(as.data.table(DF, key="t")), "t")

# data.table keyed with "b"
DT = data.table(a = 1:5, b = 1:5, x = 1:5, key = "b")
test(2309.03, key(as.data.table(DT, key="a")), "a")
test(2309.04, key(as.data.table(DT)), "b")
test(2309.05, key(as.data.table(DT, key=NULL)), NULL)
DT = data.table(a = 1:5, b = 1:5, x = 1:5, key = "b")
test(2309.03, key(as.data.table(DT, key="a")), "a")
test(2309.04, key(as.data.table(DT)), "b")
test(2309.05, key(as.data.table(DT, key=NULL)), NULL)

# non-keyed data.table
DT = data.table(a = 1:5, b = 1:5, x = 1:5)
test(2309.06, key(as.data.table(DT, key="a")), "a")
test(2309.07, key(as.data.table(DT)), NULL)
test(2309.08, key(as.data.table(DT, key=NULL)), NULL)
test(2309.06, key(as.data.table(DT, key="a")), "a")
test(2309.07, key(as.data.table(DT)), NULL)
test(2309.08, key(as.data.table(DT, key=NULL)), NULL)

# as.data.table(x, keep.rownames=TRUE) keeps rownames for class(x)==c("*", "data.frame")
df = structure(list(i = 1:2), class = c("tbl", "data.frame"), row.names = c("a","b"))
Expand Down Expand Up @@ -21228,3 +21228,12 @@ if (test_R.utils) local({
})
test(2320.2, fread(tmp), error="R.utils::decompressFile failed to decompress", warning="invalid")
})

# Create a data.table when one vector is transposed doesn't respect the name defined by user #4124
test(2321.1, DT <- data.table(a=1:2, b=matrix(1:2)), data.table(a=1:2, b=1:2))
test(2321.2, names(DT), names(data.frame(a=1:2, b=matrix(1:2))))
test(2321.3, DT <- data.table(a=integer(), b=matrix(1L, nrow=0L, ncol=1L)), data.table(a=integer(), b=integer()))
test(2321.4, names(DT), names(data.frame(a=integer(), b=matrix(1L, nrow=0L, ncol=1L))))
## but respect named column vectors
test(2321.5, DT <- data.table(a=1:2, cbind(b=3:4)), data.table(a=1:2, b=3:4))
test(2321.6, names(DT), names(data.frame(a=1:2, cbind(b=3:4))))
Loading