I like the column spec workflow as described in this RStudio blog post . Basically, you can get the column specification after import read_csv, and then save it for later use. For example, from this post:
mtcars2 <- read_csv(readr_example("mtcars.csv"))
mtcars_spec <- write_rds(spec(mtcars2), "mtcars2-spec.rds")
mtcars2 <- read_csv(
readr_example("mtcars.csv"),
col_types = read_rds("mtcars2-spec.rds")
)
Unfortunately, the specification objects themselves are lists with attributes, but they do not correspond to the various column specifications provided by the function read_csvusing the parametercol_types
> mtcars_spec$cols$cyl
<collector_integer>
> str(mtcars_spec$cols$cyl)
list()
- attr(*, "class")= chr [1:2] "collector_integer" "collector"
> class(mtcars_spec)
[1] "col_spec"
Also, .rds files are ugly for editing on Windows (at least for me).
col_spec (, ). , , , :
attr(mtcars_spec$cols$cyl,"class")[1] = "collector_skip"` # this worked!
> mtcars_spec
cols(
mpg = col_double(),
cyl = col_skip(),
disp = col_double(),
hp = col_integer(),
drat = col_double(),
wt = col_double(),
qsec = col_double(),
vs = col_integer(),
am = col_integer(),
gear = col_integer(),
carb = col_integer()
)
. , , , mtcars$cyl? , , ? , <collector_date> .