tibble
tibble copied to clipboard
Add data type argument to rownames_to_columns
Please could you consider adding an argument to the tibble::rownames_to_column
function that allows the user to specify the expected data type of the column that is created. Currently (version 3.0.5), the new column is always of type character, presumably because rownames(dataframe)
always returns a character vector. However, it seems reasonable to assume that many users would want row names that look like numbers to be converted to a column of type numeric. In my case, I often use rownames_to_column
for time series data where the row names are dates, and I consequently have to follow it up with mutate(date = as.Date(date))
in order to convert the resulting column to the date type.
Thanks. I see that a ptype
argument might be useful, I'm not sure we should support it though. There would be some symmetry with column_to_rownames()
which calls as.character()
.
For now, how about a helper function:
library(tidyverse)
data <- data.frame(a = 1:3, row.names = Sys.Date() + 1:3)
rownames_to_date_column <- function(x, var = "rowname") {
data %>%
rownames_to_column() %>%
as_tibble() %>%
mutate(across(!!rlang::as_name(var), as.Date))
}
data %>%
rownames_to_date_column()
#> # A tibble: 3 x 2
#> rowname a
#> <date> <int>
#> 1 2021-02-24 1
#> 2 2021-02-25 2
#> 3 2021-02-26 3
Created on 2021-02-23 by the reprex package (v1.0.0)
Thanks for your response, @krlmlr.
I see that a
ptype
argument might be useful, I'm not sure we should support it though. There would be some symmetry withcolumn_to_rownames()
which callsas.character()
.
I don't quite agree. Row names must be character vectors, so it makes sense that column_to_rownames()
calls as.character()
. This is obviously not true when converting from row names to columns i.e. the operations are not symmetric in this sense. Seems like a strange reason to limit functionality.
Thanks for suggesting a helper function. However, I was looking for a solution that involves less code, not more. Without a ptype
argument, it seems less cumbersome to simply change the dataype with a mutate
operation:
data %>%
rownames_to_column('date') %>%
mutate(date = as.Date(date))
Happy to review a pull request. You can use vctrs::vec_cast()
to convert the type.