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

Add data type argument to rownames_to_columns #856

Open
miguellacerda opened this issue Feb 17, 2021 · 3 comments
Open

Add data type argument to rownames_to_columns #856

miguellacerda opened this issue Feb 17, 2021 · 3 comments
Labels
help wanted ❤️ we'd love your help!
Milestone

Comments

@miguellacerda
Copy link

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.

@krlmlr
Copy link
Member

krlmlr commented Feb 23, 2021

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)

@miguellacerda
Copy link
Author

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 with column_to_rownames() which calls as.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))

@krlmlr
Copy link
Member

krlmlr commented Feb 23, 2021

Happy to review a pull request. You can use vctrs::vec_cast() to convert the type.

@krlmlr krlmlr added the help wanted ❤️ we'd love your help! label Apr 15, 2021
@krlmlr krlmlr pinned this issue Apr 16, 2021
@krlmlr krlmlr added this to the 3.1.7 milestone Dec 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted ❤️ we'd love your help!
Projects
None yet
Development

No branches or pull requests

2 participants