Repository navigation
data.table(keep.rownames = TRUE) should preserve names from vectors #1916
Description
Activity
I think this is very close in intent to #1244.
For the record,
data.frameonly assigns the names of the first passed vector:data.frame( x = setNames((1:3) ^ 3, month.abb[1:3]), y = setNames(nm = 3:1) ) # x y # Jan 1 3 # Feb 8 2 # Mar 27 1@MichaelChirico Thanks, I hadn't spotted that issue.
FYI,
data.frametakes the row names from the first vector that is named. For example, if the first vector isn't named but the second one is, it takes names from the second one.data.frame( x = (1:3) ^ 3, y = setNames(nm = 3:1) )
Reacted by Michael Chirico and MukulHi @MichaelChirico @tdhock ,
Can I work on this. My approach is simply to mimicdata.frame()behavior by detecting the first named vector inas.data.table.list()and extracting its names as arownamescolumn whenkeep.rownames = TRUE.
So inas.data.table.Radd logic to-- find the first named atomic vector
- extract and remove its names
- then prepend names as first column
Does this seems right to you?
Thanks Mukul, I think for this feature, consistency with
data.frameis important, so with that in mind I'd- Study the behavior of
data.frame()here to be sure you've understood how/when row names are extracted from its inputs. - Do the same for
as.data.frame()methods, e.g.as.data.frame.matrix(), to see if this feature should also extend to e.g.as.data.table.list()(which is the workhorses behinddata.table()). That's important for knowing where to apply the fix -- in theas.data.tablemethod, or just indata.table()itself. - Keep in mind
keep.rownames="col"typically creates the columncolto store the row names, which we should support for consistency.
Reacted by Mukul- Study the behavior of
Thanks @MichaelChirico
I find thatas.data.frame.list()handles the extraction fordata.frameto automatically extract names from the first named vector as row names.
And also bothdata.frame(x = named_vector)andas.data.frame(list(x = named_vector))produce identical results so I believe fix belongs to as.data.table.lists() as data.table() callsas.data.table.list()and alsoas.data.table()methods also route throughas.data.table.list().
In Current matrix implementationkeep.rownames=TRUEcreates "rn" andkeep.rownames="col"creates "col" so I will follow the exact pattern.
If this seems right, can I open a PR?Great start, I think you're ready to proceed to PR.
Keep in mind OP's follow-up comment too:
FYI, data.frame takes the row names from the first vector that is named. For example, if the first vector isn't named but the second one is, it takes names from the second one.
Reacted by Mukul- added a commit that references this issue
on May 22, 2026
data.frames will form row names from the names of vector inputs.I expected
data.tableto do something similar whenkeep.rownames = TRUE. That is I expected that these two pieces of code would be equivalent:Currently
keep.rownames = TRUEseems to only apply to matrix or data.frame inputs, rather than vectors. Changing the behavior so thatkeep.rownames = TRUEmimicsdata.framebehavior seems desirable.