Skip to content

Commit a90fe47

Browse files
add tests
1 parent c143dab commit a90fe47

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

R/as.data.table.R

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,30 @@ as.data.table.list = function(x,
134134
origListNames = if (missing(.named)) names(x) else NULL # as.data.table called directly, not from inside data.table() which provides .named, #3854
135135
empty_atomic = FALSE
136136

137-
#Handle keep.rownames for vectors (mimicking data.frame behavior)
137+
# Handle keep.rownames for vectors (mimicking data.frame behavior)
138138
vector_rownames = NULL
139-
if(!identical(keep.rownames, FALSE)) {
140-
for(i in seq_len(n)){
141-
xi = x[[i]]
142-
if (!is.null(xi) && is.atomic(xi) && !is.null(names(xi)) && is.null(dim(xi)) && length(names(xi)) > 0) {
143-
valid_names = names(xi)
144-
if(any(nzchar(valid_names))) {
139+
check_rownames = !isFALSE(keep.rownames)
140+
141+
for (i in seq_len(n)) {
142+
xi = x[[i]]
143+
if (is.null(xi)) next # eachncol already initialized to 0 by integer() above
144+
if (check_rownames && is.null(vector_rownames)) {
145+
# Check for named vectors
146+
if (is.atomic(xi) && !is.null(names(xi)) && is.null(dim(xi))) {
147+
valid_names = names(xi)
148+
if (any(nzchar(valid_names))) {
145149
vector_rownames = valid_names
146150
x[[i]] = unname(xi)
147-
break
148151
}
149152
}
150-
}
151-
}
152-
for (i in seq_len(n)) {
153-
xi = x[[i]]
154-
if (is.null(xi)) next # eachncol already initialized to 0 by integer() above
153+
# Check for data.frames or matrices with explicit rownames
154+
else if (!is.null(dim(xi)) && !is.null(rownames(xi))) {
155+
valid_names = rownames(xi)
156+
if (any(nzchar(valid_names))) {
157+
vector_rownames = valid_names
158+
}
159+
}
160+
}
155161
if (!is.null(dim(xi)) && missing.check.names) check.names=TRUE
156162
if ("POSIXlt" %chin% class(xi)) {
157163
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.")
@@ -161,7 +167,7 @@ as.data.table.list = function(x,
161167
if (is.matrix(xi) && NCOL(xi)<=1L && is.null(colnames(xi))) { # 1 column matrix naming #4124
162168
xi = x[[i]] = c(xi)
163169
} else {
164-
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
170+
xi = x[[i]] = as.data.table(xi, keep.rownames=FALSE) # we will never allow a matrix to be a column; always unpack the columns
165171
}
166172
}
167173
# else avoid dispatching to as.data.table.data.table (which exists and copies)

inst/tests/tests.Rraw

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21371,3 +21371,16 @@ x <- c(1, 2, 3)
2137121371
y <- setNames(c(4, 5, 6), c("A", "B", "C"))
2137221372
test(2329.1, as.data.table.list(list(x, y), keep.rownames=TRUE), data.table(rn=c("A", "B", "C"), V1=c(1, 2, 3), V2=c(4, 5, 6)))
2137321373
test(2329.2, as.data.table.list(list(x, y), keep.rownames="custom"), data.table(custom=c("A", "B", "C"), V1=c(1, 2, 3), V2=c(4, 5, 6)))
21374+
test(2329.3, as.data.table.list(list(y, x), keep.rownames=TRUE), data.table(rn=c("A", "B", "C"), V1=c(4, 5, 6), V2=c(1, 2, 3)))
21375+
21376+
# Behavior under data.frame()
21377+
test(2329.4, as.data.table(data.frame(x, y), keep.rownames=TRUE), data.table(rn=c("A", "B", "C"), V1=c(1, 2, 3), V2=c(4, 5, 6)))
21378+
test(2329.5, as.data.table(data.frame(y, x), keep.rownames=TRUE), data.table(rn=c("A", "B", "C"), V1=c(4, 5, 6), V2=c(1, 2, 3)))
21379+
21380+
a <- setNames(c(7, 8, 9), c("", "", "")) # test condition about any(nzchar(valid_names))
21381+
test(2329.6, as.data.table.list(list(a), keep.rownames=TRUE), data.table(V1=c(7, 8, 9)))
21382+
b <- setNames(c(10, 11, 12), c("", "B", ""))
21383+
test(2329.7, as.data.table.list(list(b), keep.rownames=TRUE), data.table(rn=c("", "B", ""), V1=c(10, 11, 12)))
21384+
21385+
DF <- data.frame(row.names = letters[1:6], V = 1:6) # Test data.frame with explicit rownames
21386+
test(2329.8, as.data.table(list(a = 6:1, DF), keep.rownames=TRUE), data.table(rn=c("a", "b", "c", "d", "e", "f"), a=c(6, 5, 4, 3, 2, 1), V=c(1, 2, 3, 4, 5, 6)))

0 commit comments

Comments
 (0)