col_types() repetition

Is there a more elegant way to do this, other than repeating myself for a few hundred differing column types? Can I pass lists as arguments to col_types here?

mydata <- read_csv("data.csv", col_types = cols_only(
    name = col_character(),
    birthyr = col_date(format = "%Y"),
    success = col_factor(),
    attempts = col_factor(),
    birthdy = col_date(format = "%m/%d/%Y"),
    firstyr = col_date(format = "%Y"))
  )
  

I use the following approach

library(readr)

fct_cols <- c("success", "attempts")
yr_cols <- c("birthyr", "firstyr")
mdy_cols <- c("birthdy")
chr_cols <- c("name")

create_spec <- function(col_format, names) {
  col_spec <- rep_len(list(col_format), length.out = length(names))
  setNames(col_spec, names)
}

col_spec_list <- c(
  create_spec(col_character(), names = chr_cols),
  create_spec(col_factor(), names = fct_cols),
  create_spec(col_date(format = "%Y"), names = yr_cols),
  create_spec(col_date(format = "%m/%d/%Y"), names = mdy_cols)
)
col_spec <- do.call(cols_only, col_spec_list)
col_spec
#> cols_only(
#>   name = col_character(),
#>   success = col_factor(levels = NULL, ordered = FALSE, include_na = FALSE),
#>   attempts = col_factor(levels = NULL, ordered = FALSE, include_na = FALSE),
#>   birthyr = col_date(format = "%Y"),
#>   firstyr = col_date(format = "%Y"),
#>   birthdy = col_date(format = "%m/%d/%Y")
#> )

# or the tidyverse alternative to `do.call()`
rlang::exec(cols_only, !!!col_spec_list)
#> cols_only(
#>   name = col_character(),
#>   success = col_factor(levels = NULL, ordered = FALSE, include_na = FALSE),
#>   attempts = col_factor(levels = NULL, ordered = FALSE, include_na = FALSE),
#>   birthyr = col_date(format = "%Y"),
#>   firstyr = col_date(format = "%Y"),
#>   birthdy = col_date(format = "%m/%d/%Y")
#> )

Created on 2020-10-26 by the reprex package (v0.3.0)

I think one gets a good overview when defining all the columns that are of the same type in one place and it is less repetitive.

4 Likes

Thank you! I figured it was something straightforward, but I had to get it done rather than get it done elegantly. I guess it is time to dig into rlang().

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

Done, thanks for the heads up, Mara!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.