Readr - how to update col_spec object from spec ()

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"))
#> Parsed with column specification:
#> cols(
#>   mpg = col_double(),
#>   cyl = col_integer(),
#>   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()
#> )
# Once you've figured out the correct types
mtcars_spec <- write_rds(spec(mtcars2), "mtcars2-spec.rds")

# Every subsequent load
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> .

+4

Source: https://habr.com/ru/post/1652396/


All Articles