@@ -133,6 +133,22 @@ as.data.table.list = function(x,
133133 missing.check.names = missing(check.names )
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
136+
137+ # Handle keep.rownames for vectors (mimicking data.frame behavior)
138+ 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 ))) {
145+ vector_rownames = valid_names
146+ x [[i ]] = unname(xi )
147+ break
148+ }
149+ }
150+ }
151+ }
136152 for (i in seq_len(n )) {
137153 xi = x [[i ]]
138154 if (is.null(xi )) next # eachncol already initialized to 0 by integer() above
@@ -200,6 +216,13 @@ as.data.table.list = function(x,
200216 }
201217 if (any(vnames == " .SD" )) stopf(" A column may not be called .SD. That has special meaning." )
202218 if (check.names ) vnames = make.names(vnames , unique = TRUE )
219+
220+ # Add rownames column when vector names were found
221+ if (! is.null(vector_rownames )){
222+ rn_name = if (is.character(keep.rownames )) keep.rownames [1L ] else " rn"
223+ ans = c(list (recycle(vector_rownames , nrow )), ans )
224+ vnames = c(rn_name , vnames )
225+ }
203226 setattr(ans , " names" , vnames )
204227 setDT(ans , key = key ) # copy ensured above; also, setDT handles naming
205228 if (length(origListNames )== length(ans )) setattr(ans , " names" , origListNames ) # PR 3854 and tests 2058.15-17
0 commit comments