Skip to content

should type_convert() erase the 'spec' attribute? #1032

@wibeasley

Description

@wibeasley

When readr::type_convert() is applied to a data.frame, should it update the 'spec' attribute? Currently, it does not, and potentially returns a misleading result.

library(magrittr); library(readr)
d1 <- 
  read_csv(
    readr_example("mtcars.csv"),
    col_types = cols(.default = col_character())
  )
str(d1)

After the initial creation, the data.frame's spec attribute is correct (i.e., all columns are character).

Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame':	32 obs. of  11 variables:
 $ mpg : chr  "21" "21" "22.8" "21.4" ...
 ...
 $ carb: chr  "4" "4" "1" "1" ...
 - attr(*, "spec")=
  .. cols(
  ..   .default = col_character(),
  ..   mpg = col_character(),
  ..   cyl = col_character(),
  ...
  ..   carb = col_character()
  .. )

Then type_convert() correctly converts everything to doubles.

d2 <- data %>% 
  type_convert()
Parsed with column specification:
cols(
  mpg = col_double(),
  cyl = col_double(),
  ...
  carb = col_double()
)

But the spec still shows the character vectors.

spec(d2)
cols(
  .default = col_character(),
  mpg = col_character(),
  cyl = col_character(),
  ...
  carb = col_character()
)

BTW, I really like this function. It was nice when I was stacking a bunch of dataframe on top of each other, but some columns in some sub-dataframes were empty to the data types weren't compatible during the stack.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions