Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as.data.table.data.frame keep.rownames & key #4468

Closed
MichaelChirico opened this issue May 21, 2020 · 3 comments · Fixed by #4469
Closed

as.data.table.data.frame keep.rownames & key #4468

MichaelChirico opened this issue May 21, 2020 · 3 comments · Fixed by #4469

Comments

@MichaelChirico
Copy link
Member

MichaelChirico commented May 21, 2020

This works fine:

as.data.table(mtcars, keep.rownames = TRUE, key = 'rn')

But it's a bit arcane to know that rn is going to be the rownames' name. More readable might be:

as.data.table(mtcars, keep.rownames = 'model', key = 'model'

But that's an error:

Error in setkeyv(ans, key) : some columns are not in the data.table: model

Haven't looked yet but it must be setkeyv is applied before the setnames.

Note that this approach already works for setDT:

setDT(DT, keep.rownames = 'model', key = 'model')
@moodymudskipper
Copy link

It might add confusion, but thinking out loud maybe key = TRUE could use the row names as a key by default, or the first column if no row names (the id column will usually be first). This way your first call could be abbreviated to as.data.table(mtcars, key = TRUE)

@MichaelChirico
Copy link
Member Author

Hmm but when I read as.data.table(mtcars, key=TRUE), actually I think of the answer being setkey(as.data.tabel(mtcars)); setkey keys on all columns by default.

@moodymudskipper
Copy link

Right, there could also be special values for key but that might be too much potential confusion to spare a few characters.

@mattdowle mattdowle added this to the 1.14.1 milestone Aug 18, 2021
@jangorecki jangorecki modified the milestones: 1.14.9, 1.15.0 Oct 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants