Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
23 changes: 23 additions & 0 deletions R/as.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ 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)
vector_rownames = NULL
if(!identical(keep.rownames, FALSE)) {
for(i in seq_len(n)){
xi = x[[i]]
if (!is.null(xi) && is.atomic(xi) && !is.null(names(xi)) && is.null(dim(xi)) && length(names(xi)) > 0) {
valid_names = names(xi)
if(any(nzchar(valid_names))) {
vector_rownames = valid_names
x[[i]] = unname(xi)
break
Copy link
Member

Choose a reason for hiding this comment

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

I am leaning towards merging this logic into the below loop. We can write check_rownames = !isFALSE(keep.rownames) and then the marginal cost of checking if (check_rownames && ...) is low. We can replace the early break with checking is.null(vector_rownames). WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree

}
}
}
}
for (i in seq_len(n)) {
xi = x[[i]]
if (is.null(xi)) next # eachncol already initialized to 0 by integer() above
Expand Down Expand Up @@ -200,6 +216,13 @@ 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(vector_rownames)){
rn_name = if (is.character(keep.rownames)) keep.rownames[1L] else "rn"
ans = c(list(recycle(vector_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
7 changes: 7 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -21365,3 +21365,10 @@ test(2328.1, levels(droplevels(DT)$f), character())
DT[, i := integer()]
DT[, f2 := factor()]
test(2328.2, droplevels(DT), data.table(f=factor(), i=integer(), f2=factor()))

# Row name extraction from multiple vectors, #7136
x <- c(1, 2, 3)
y <- setNames(c(4, 5, 6), c("A", "B", "C"))
test(2329.1, as.data.table(list(x, y), keep.rownames=TRUE), data.table(rn=c("A", "B", "C"), V1=c(1, 2, 3), V2=c(4, 5, 6)))
test(2329.2, as.data.table(list(x, y), keep.rownames="custom"), data.table(custom=c("A", "B", "C"), V1=c(1, 2, 3), V2=c(4, 5, 6)))
Copy link
Member

Choose a reason for hiding this comment

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

Thanks! Let's make the test suite more extensive:

  • Test also list(y, x)
  • Test the behavior under data.frame(), not just as.data.table
  • Test your condition about any(nzchar(valid_names))

I also don't think we've matched data.frame behavior yet, c.f.

DF = data.frame(row.names = letters, V = 1:26)
head(rownames(data.frame(a = 26:1, DF)))
# [1] "a" "b" "c" "d" "e" "f"


Loading