Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
7539668
Added logic to s.data.table.list() to preserve names from vectors
Mukulyadav2004 Jul 7, 2025
3cfc61b
remove trail whit space
Mukulyadav2004 Jul 7, 2025
7d1e543
add coverage tests
Mukulyadav2004 Jul 7, 2025
11485c9
replace X and Y
Mukulyadav2004 Jul 7, 2025
4088852
result test
Mukulyadav2004 Jul 7, 2025
c143dab
tests
Mukulyadav2004 Jul 7, 2025
6775ce1
corrected test output
Mukulyadav2004 Jul 7, 2025
b825415
use as.data.table in tests
Mukulyadav2004 Jul 8, 2025
9d6f99a
use isFalse
Mukulyadav2004 Jul 8, 2025
68cc421
add classed error conditions
Mukulyadav2004 Jul 8, 2025
11acfe6
ws style
MichaelChirico Jul 8, 2025
26160e2
rm redundant condition
MichaelChirico Jul 8, 2025
92f281f
missed ws change
MichaelChirico Jul 8, 2025
7041d8d
invalid_input in place of invalid_type
Mukulyadav2004 Jul 8, 2025
0a583fa
right place invalid_input
Mukulyadav2004 Jul 8, 2025
42d63ad
typo and unsortable in place of unsupported
Mukulyadav2004 Jul 8, 2025
0602e72
specify join type
Mukulyadav2004 Jul 8, 2025
b6cc6ac
merge our loop which checks for vector rowname extraction in below
Mukulyadav2004 Jul 8, 2025
3454fcf
added logic for handling data.frame
Mukulyadav2004 Jul 8, 2025
f3e5a0e
add tests
Mukulyadav2004 Jul 8, 2025
a90fe47
add tests
Mukulyadav2004 Jul 8, 2025
babc657
merger master
Mukulyadav2004 Jul 9, 2025
aea5676
Merge branch 'master' of https://github.com/Rdatatable/data.table int…
Mukulyadav2004 Jul 9, 2025
4443f3c
resolve merge
Mukulyadav2004 Jul 9, 2025
9710d43
remove duplicate
Mukulyadav2004 Jul 9, 2025
7fe757d
remove list from as.data.table
Mukulyadav2004 Jul 9, 2025
555fcc7
added vignettes
Mukulyadav2004 Jul 9, 2025
cc12e82
Merge branch 'issue#5913' of https://github.com/Rdatatable/data.table…
Mukulyadav2004 Jul 9, 2025
1bb1e46
rm ws
MichaelChirico Jul 9, 2025
8956d2e
try to simplify
MichaelChirico Jul 9, 2025
ed472a2
fix tests
MichaelChirico Jul 9, 2025
eb50409
restore
MichaelChirico Jul 9, 2025
7a79a18
try and handle "inner" row names from matrix case
MichaelChirico Jul 9, 2025
bdc6b99
rm vestigial
MichaelChirico Jul 9, 2025
b9d879e
fix
MichaelChirico Jul 9, 2025
13ed346
simplify tests
MichaelChirico Jul 9, 2025
02b9ac8
Merge branch 'issue#5913' of https://github.com/Rdatatable/data.table…
Mukulyadav2004 Jul 9, 2025
4098c0c
Merge branch 'issue_1916' of https://github.com/Rdatatable/data.table…
Mukulyadav2004 Jul 9, 2025
311f145
remove any(nzchar(nm))
Mukulyadav2004 Jul 9, 2025
5114871
remove test condition about any(nzchar(nm))
Mukulyadav2004 Jul 9, 2025
d42f1b3
update test number
Mukulyadav2004 Jul 9, 2025
118d6e1
Merge branch 'issue_1916' of https://github.com/Rdatatable/data.table…
Mukulyadav2004 Jul 10, 2025
3a8f73e
update description , news.md and add tests
Mukulyadav2004 Jul 10, 2025
6dbc414
remove unwanted changes
Mukulyadav2004 Jul 10, 2025
89e9ef4
Merge branch 'master' into issue_1916
MichaelChirico Jul 10, 2025
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 @@ -44,6 +44,8 @@

9. `isoweek()` is much faster (e.g. 20x) by re-using an implementation from {base}, [#5111](https://github.com/Rdatatable/data.table/issues/5111). Thanks @MichaelChirico for the report and PR.

10. `data.table()` and `as.data.table()` with `keep.rownames=TRUE` now extract row names from named vectors, matching `data.frame()` behavior. Names from the first named vector in the input are used to create the row names column (default name `"rn"` or custom name via `keep.rownames="column_name"`), [#1916](https://github.com/Rdatatable/data.table/issues/1916). Thanks to @richierocks for the feature request and @Mukulyadav2004 for the implementation.

### BUG FIXES

1. Custom binary operators from the `lubridate` package now work with objects of class `IDate` as with a `Date` subclass, [#6839](https://github.com/Rdatatable/data.table/issues/6839). Thanks @emallickhossain for the report and @aitap for the fix.
Expand Down
29 changes: 29 additions & 0 deletions R/as.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,26 @@ as.data.table.list = function(x,
missing.check.names = missing(check.names)
origListNames = if (missing(.named)) names(x) else NULL # as.data.table called directly, not from inside data.table() which provides .named, #3854
empty_atomic = FALSE

# Handle keep.rownames for vectors (mimicking data.frame behavior)
rownames_ = NULL
check_rownames = !isFALSE(keep.rownames)

for (i in seq_len(n)) {
xi = x[[i]]
if (is.null(xi)) next # eachncol already initialized to 0 by integer() above
if (check_rownames && is.null(rownames_)) {
if (is.null(dim(xi))) {
if (!is.null(nm <- names(xi))) {
rownames_ = nm
x[[i]] = unname(xi)
}
} else {
if (!is.null(nm <- rownames(xi))) {
rownames_ = nm
}
}
}
if (!is.null(dim(xi)) && missing.check.names) check.names=TRUE
if ("POSIXlt" %chin% class(xi)) {
warningf("POSIXlt column type detected and converted to POSIXct. We do not recommend use of POSIXlt at all because it uses 40 bytes to store one date.")
Expand Down Expand Up @@ -203,6 +220,18 @@ as.data.table.list = function(x,
}
if (any(vnames==".SD")) stopf("A column may not be called .SD. That has special meaning.")
if (check.names) vnames = make.names(vnames, unique=TRUE)

# Add rownames column when vector names were found
if (!is.null(rownames_)) {
rn_name = if (is.character(keep.rownames)) keep.rownames[1L] else "rn"
if (!is.na(idx <- chmatch(rn_name, vnames)[1L])) {
ans = c(list(ans[[idx]]), ans[-idx])
vnames = c(vnames[idx], vnames[-idx])
} else {
ans = c(list(recycle(rownames_, nrow)), ans)
vnames = c(rn_name, vnames)
}
}
setattr(ans, "names", vnames)
setDT(ans, key=key) # copy ensured above; also, setDT handles naming
if (length(origListNames)==length(ans)) setattr(ans, "names", origListNames) # PR 3854 and tests 2058.15-17
Expand Down
20 changes: 20 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -21406,3 +21406,23 @@ dt = data.table(x = 123456, y = "wide_string")
test(2329.2, print(dt, col.names = "none"), output = "1: 123456 wide_string\n")
dt = data.table(a = NA_integer_, b = NaN)
test(2329.3, print(dt, col.names = "none"), output = "1: NA NaN\n")

# Row name extraction from multiple vectors, #7136
x <- 1:3
y <- setNames(4:6, c("A", "B", "C"))
test(2330.1, as.data.table(list(x, y), keep.rownames=TRUE), data.table(rn=c("A", "B", "C"), V1=1:3, V2=4:6))
test(2330.2, as.data.table(list(x, y), keep.rownames="custom"), data.table(custom=c("A", "B", "C"), V1=1:3, V2=4:6))
test(2330.3, as.data.table(list(y, x), keep.rownames=TRUE), data.table(rn=c("A", "B", "C"), V1=4:6, V2=1:3))

# Behavior under data.frame()
test(2330.4, as.data.table(data.frame(x, y), keep.rownames=TRUE), data.table(rn=c("A", "B", "C"), x=1:3, y=4:6))
test(2330.5, as.data.table(data.frame(y, x), keep.rownames=TRUE), data.table(rn=c("A", "B", "C"), y=4:6, x=1:3))

DF <- data.frame(row.names = letters[1:6], V = 1:6) # Test data.frame with explicit rownames
test(2330.6, as.data.table(list(a = 6:1, DF), keep.rownames=TRUE), data.table(rn=letters[1:6], a=6:1, V=1:6))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than remove the rest with all-empty names, let's test the expected behavior in that case as well.

Please also add a test of list(M) for empty-rowname'd matrix input


z <- setNames(1:3, rep("", 3)) # vector with all-empty names # behaviour with all-empty row names
test(2330.7, as.data.table(list(z), keep.rownames=TRUE), data.table(rn=rep("", 3), V1=1:3))

M <- matrix(1:6, nrow=3, dimnames=list(rep("", 3), c("V1", "V2"))) # test of list(M) for empty-rowname'd matrix input
test(2330.8, as.data.table(list(M), keep.rownames=TRUE), data.table(rn=rep("", 3), V1=1:3, V2=4:6))
2 changes: 1 addition & 1 deletion man/as.data.table.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ is.data.table(x)
}
\arguments{
\item{x}{An R object.}
\item{keep.rownames}{Default is \code{FALSE}. If \code{TRUE}, adds the input object's names as a separate column named \code{"rn"}. \code{keep.rownames = "id"} names the column \code{"id"} instead.}
\item{keep.rownames}{Default is \code{FALSE}. If \code{TRUE}, adds the input object's names as a separate column named \code{"rn"}. \code{keep.rownames = "id"} names the column \code{"id"} instead. For lists and when calling \code{data.table()}, names from the first named vector are extracted and used as row names, similar to \code{data.frame()} behavior.}
\item{key}{ Character vector of one or more column names which is passed to \code{\link{setkeyv}}. }
\item{sorted}{logical used in \emph{array} method, default \code{TRUE} is overridden when \code{key} is provided. }
\item{value.name}{character scalar used in \emph{array} method, default \code{"value"}.}
Expand Down
Loading