You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
data.table(keep.rownames = TRUE) should preserve names from vectors #1916
Currently keep.rownames = TRUE seems to only apply to matrix or data.frame inputs, rather than vectors. Changing the behavior so that keep.rownames = TRUE mimics data.frame behavior seems desirable.
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.
Hi @MichaelChirico@tdhock ,
Can I work on this. My approach is simply to mimic data.frame() behavior by detecting the first named vector in as.data.table.list() and extracting its names as a rownames column when keep.rownames = TRUE.
So in as.data.table.R add logic to-
Thanks Mukul, I think for this feature, consistency with data.frame is 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 behind data.table()). That's important for knowing where to apply the fix -- in the as.data.table method, or just in data.table() itself.
Keep in mind keep.rownames="col" typically creates the column col to store the row names, which we should support for consistency.
Thanks @MichaelChirico
I find that as.data.frame.list() handles the extraction for data.frame to automatically extract names from the first named vector as row names.
And also both data.frame(x = named_vector) and as.data.frame(list(x = named_vector)) produce identical results so I believe fix belongs to as.data.table.lists() as data.table() calls as.data.table.list() and also as.data.table() methods also route through as.data.table.list().
In Current matrix implementation keep.rownames=TRUE creates "rn" and keep.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.
Activity
MichaelChirico commentedon Nov 17, 2016
I think this is very close in intent to #1244.
For the record,
data.frameonly assigns the names of the first passed vector:richierocks commentedon Nov 17, 2016
@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.Mukulyadav2004 commentedon Jul 6, 2025
Hi @MichaelChirico @tdhock ,
Can I work on this. My approach is simply to mimic
data.frame()behavior by detecting the first named vector inas.data.table.list()and extracting its names as arownamescolumn whenkeep.rownames = TRUE.So in
as.data.table.Radd logic to-Does this seems right to you?
MichaelChirico commentedon Jul 6, 2025
Thanks Mukul, I think for this feature, consistency with
data.frameis important, so with that in mind I'ddata.frame()here to be sure you've understood how/when row names are extracted from its inputs.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.rownames="col"typically creates the columncolto store the row names, which we should support for consistency.Mukulyadav2004 commentedon Jul 7, 2025
Thanks @MichaelChirico
I find that
as.data.frame.list()handles the extraction fordata.frameto automatically extract names from the first named vector as row names.And also both
data.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 implementation
keep.rownames=TRUEcreates "rn" andkeep.rownames="col"creates "col" so I will follow the exact pattern.If this seems right, can I open a PR?
MichaelChirico commentedon Jul 7, 2025
Great start, I think you're ready to proceed to PR.
Keep in mind OP's follow-up comment too:
(textproc/R-data.table) Updated 1.16.4 to 1.18.4, Fix build against 4…
(textproc/R-data.table) Updated 1.16.4 to 1.18.4, Fix build against 4…