-
Notifications
You must be signed in to change notification settings - Fork 1k
Row Name Extraction for data.table() with keep.rownames #7136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
7539668
3cfc61b
7d1e543
11485c9
4088852
c143dab
6775ce1
b825415
9d6f99a
68cc421
11acfe6
26160e2
92f281f
7041d8d
0a583fa
42d63ad
0602e72
b6cc6ac
3454fcf
f3e5a0e
a90fe47
babc657
aea5676
4443f3c
9710d43
7fe757d
555fcc7
cc12e82
1bb1e46
8956d2e
ed472a2
eb50409
7a79a18
bdc6b99
b9d879e
13ed346
02b9ac8
4098c0c
311f145
5114871
d42f1b3
118d6e1
3a8f73e
6dbc414
89e9ef4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)){ | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| xi = x[[i]] | ||
| if (!is.null(xi) && is.atomic(xi) && !is.null(names(xi)) && is.null(dim(xi)) && length(names(xi)) > 0) { | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| valid_names = names(xi) | ||
| if(any(nzchar(valid_names))) { | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| vector_rownames = valid_names | ||
| x[[i]] = unname(xi) | ||
| break | ||
|
||
| } | ||
| } | ||
| } | ||
| } | ||
| for (i in seq_len(n)) { | ||
| xi = x[[i]] | ||
| if (is.null(xi)) next # eachncol already initialized to 0 by integer() above | ||
|
|
@@ -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)){ | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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))) | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.