Skip to content

Commit 4098c0c

Browse files
Merge branch 'issue_1916' of https://github.com/Rdatatable/data.table into issue_1916
2 parents 02b9ac8 + 13ed346 commit 4098c0c

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

R/as.data.table.R

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,29 +138,24 @@ as.data.table.list = function(x,
138138
empty_atomic = FALSE
139139

140140
# Handle keep.rownames for vectors (mimicking data.frame behavior)
141-
vector_rownames = NULL
141+
rownames_ = NULL
142142
check_rownames = !isFALSE(keep.rownames)
143-
143+
144144
for (i in seq_len(n)) {
145145
xi = x[[i]]
146146
if (is.null(xi)) next # eachncol already initialized to 0 by integer() above
147-
if (check_rownames && is.null(vector_rownames)) {
148-
# Check for named vectors
149-
if (is.atomic(xi) && !is.null(names(xi)) && is.null(dim(xi))) {
150-
valid_names = names(xi)
151-
if (any(nzchar(valid_names))) {
152-
vector_rownames = valid_names
147+
if (check_rownames && is.null(rownames_)) {
148+
if (is.null(dim(xi))) {
149+
if (!is.null(nm <- names(xi)) && any(nzchar(nm))) {
150+
rownames_ = nm
153151
x[[i]] = unname(xi)
154152
}
155-
}
156-
# Check for data.frames or matrices with explicit rownames
157-
else if (!is.null(dim(xi)) && !is.null(rownames(xi))) {
158-
valid_names = rownames(xi)
159-
if (any(nzchar(valid_names))) {
160-
vector_rownames = valid_names
153+
} else {
154+
if (!is.null(nm <- rownames(xi)) && any(nzchar(nm))) {
155+
rownames_ = nm
161156
}
162157
}
163-
}
158+
}
164159
if (!is.null(dim(xi)) && missing.check.names) check.names=TRUE
165160
if ("POSIXlt" %chin% class(xi)) {
166161
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.")
@@ -170,8 +165,7 @@ as.data.table.list = function(x,
170165
if (is.matrix(xi) && NCOL(xi)<=1L && is.null(colnames(xi))) { # 1 column matrix naming #4124
171166
xi = x[[i]] = c(xi)
172167
} else {
173-
xi = x[[i]] = as.data.table(xi, keep.rownames=FALSE) # we will never allow a matrix to be a column; always unpack the columns
174-
xi = x[[i]] = as.data.table(xi, keep.rownames=FALSE) # we will never allow a matrix to be a column; always unpack the columns
168+
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
175169
}
176170
}
177171
# else avoid dispatching to as.data.table.data.table (which exists and copies)
@@ -228,10 +222,15 @@ as.data.table.list = function(x,
228222
if (check.names) vnames = make.names(vnames, unique=TRUE)
229223

230224
# Add rownames column when vector names were found
231-
if (!is.null(vector_rownames)) {
225+
if (!is.null(rownames_)) {
232226
rn_name = if (is.character(keep.rownames)) keep.rownames[1L] else "rn"
233-
ans = c(list(recycle(vector_rownames, nrow)), ans)
234-
vnames = c(rn_name, vnames)
227+
if (!is.na(idx <- chmatch(rn_name, vnames)[1L])) {
228+
ans = c(list(ans[[idx]]), ans[-idx])
229+
vnames = c(vnames[idx], vnames[-idx])
230+
} else {
231+
ans = c(list(recycle(rownames_, nrow)), ans)
232+
vnames = c(rn_name, vnames)
233+
}
235234
}
236235
setattr(ans, "names", vnames)
237236
setDT(ans, key=key) # copy ensured above; also, setDT handles naming

inst/tests/tests.Rraw

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21408,20 +21408,20 @@ dt = data.table(a = NA_integer_, b = NaN)
2140821408
test(2329.3, print(dt, col.names = "none"), output = "1: NA NaN\n")
2140921409

2141021410
# Row name extraction from multiple vectors, #7136
21411-
x <- c(1, 2, 3)
21412-
y <- setNames(c(4, 5, 6), c("A", "B", "C"))
21413-
test(2330.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)))
21414-
test(2330.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)))
21415-
test(2330.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)))
21411+
x <- 1:3
21412+
y <- setNames(4:6, c("A", "B", "C"))
21413+
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))
21414+
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))
21415+
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))
2141621416

2141721417
# Behavior under data.frame()
21418-
test(2330.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)))
21419-
test(2330.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)))
21418+
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))
21419+
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))
2142021420

21421-
a <- setNames(c(7, 8, 9), c("", "", "")) # test condition about any(nzchar(valid_names))
21422-
test(2330.6, as.data.table.list(list(a), keep.rownames=TRUE), data.table(V1=c(7, 8, 9)))
21423-
b <- setNames(c(10, 11, 12), c("", "B", ""))
21424-
test(2330.7, as.data.table.list(list(b), keep.rownames=TRUE), data.table(rn=c("", "B", ""), V1=c(10, 11, 12)))
21421+
a <- setNames(7:9, c("", "", "")) # test condition about any(nzchar(valid_names))
21422+
test(2330.6, as.data.table(list(a), keep.rownames=TRUE), data.table(V1=7:9))
21423+
b <- setNames(10:12, c("", "B", ""))
21424+
test(2330.7, as.data.table(list(b), keep.rownames=TRUE), data.table(rn=c("", "B", ""), V1=10:12))
2142521425

2142621426
DF <- data.frame(row.names = letters[1:6], V = 1:6) # Test data.frame with explicit rownames
21427-
test(2330.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)))
21427+
test(2330.8, as.data.table(list(a = 6:1, DF), keep.rownames=TRUE), data.table(rn=letters[1:6], a=6:1, V=1:6))

0 commit comments

Comments
 (0)