tdawry
October 26, 2020, 1:24am
1
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
tdawry
October 26, 2020, 6:24pm
3
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().
mara
October 27, 2020, 1:07pm
4
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.
If your question has been answered, don't forget to mark the solution!
How do I mark a solution?
Find the reply you want to mark as the solution and look for the row of small gray icons at the bottom of that reply. Click the one that looks like a box with a checkmark in it:
[image]
Hovering over the mark solution button shows the label, "Select if this reply solves the problem". If you don't see the mark solution button, try clicking the three dots button ( ••• ) to expand the full set of options.
When a solution is chosen, the icon turns green and the hover label changes to: "Unselect if this reply no longer solves the problem". Success!
[solution_reply_author]
…
Thanks
tdawry
October 28, 2020, 3:33am
5
Done, thanks for the heads up, Mara!
1 Like
system
Closed
November 4, 2020, 3:33am
6
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.