Skip to content

Commit b9d879e

Browse files
fix
1 parent bdc6b99 commit b9d879e

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

R/as.data.table.R

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ as.data.table.list = function(x,
147147
if (check_rownames && is.null(rownames_)) {
148148
if (is.null(dim(xi))) {
149149
if (!is.null(nm <- names(xi)) && any(nzchar(nm))) {
150-
rn = nm
150+
rownames_ = nm
151151
x[[i]] = unname(xi)
152152
}
153153
} else {
@@ -224,15 +224,12 @@ as.data.table.list = function(x,
224224
# Add rownames column when vector names were found
225225
if (!is.null(rownames_)) {
226226
rn_name = if (is.character(keep.rownames)) keep.rownames[1L] else "rn"
227-
ans = c(list(recycle(rownames_, nrow)), ans)
228-
vnames = c(rn_name, vnames)
229-
} else if (check_rownames) {
230-
# case like data.table(a = 1, data.frame(b = 2, row.names='c')) where expanding the inner DF picks up the row names --
231-
# we want to bump the resulting column to the front of the output
232-
rn_name = if (is.character(keep.rownames)) keep.rownames[1L] else "rn"
233-
if (!is.na(idx <- chmatch(rn_name, names(ans))[1L]) && idx != 1L) {
234-
ans = c(ans[[idx]], ans[-idx])
227+
if (!is.na(idx <- chmatch(rn_name, vnames)[1L])) {
228+
ans = c(list(ans[[idx]]), ans[-idx])
235229
vnames = c(vnames[idx], vnames[-idx])
230+
} else {
231+
ans = c(list(recycle(rownames_, nrow)), ans)
232+
vnames = c(rn_name, vnames)
236233
}
237234
}
238235
setattr(ans, "names", vnames)

inst/tests/tests.Rraw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21424,4 +21424,4 @@ b <- setNames(c(10, 11, 12), c("", "B", ""))
2142421424
test(2330.7, as.data.table(list(b), keep.rownames=TRUE), data.table(rn=c("", "B", ""), V1=c(10, 11, 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=1:6))
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=6:1, V=1:6))

0 commit comments

Comments
 (0)