Read all CSV columns as character

I'm trying to read in all columns from a CSV as character type with either readr or vroom, as I need to wrangle data further. This post: https://github.com/tidyverse/readr/issues/148 is quite old and when I try col_types = cols(.default = "c"), it seems to be ignored. Is there a simple approach to this now?

1 Like

That is still correct.

library(readr)
str(
  read_csv(readr_example("mtcars.csv"), col_types = cols(.default = "c"))
)
#> tibble [32 × 11] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
#>  $ mpg : chr [1:32] "21" "21" "22.8" "21.4" ...
#>  $ cyl : chr [1:32] "6" "6" "4" "6" ...
#>  $ disp: chr [1:32] "160" "160" "108" "258" ...
#>  $ hp  : chr [1:32] "110" "110" "93" "110" ...
#>  $ drat: chr [1:32] "3.9" "3.9" "3.85" "3.08" ...
#>  $ wt  : chr [1:32] "2.62" "2.875" "2.32" "3.215" ...
#>  $ qsec: chr [1:32] "16.46" "17.02" "18.61" "19.44" ...
#>  $ vs  : chr [1:32] "0" "0" "1" "1" ...
#>  $ am  : chr [1:32] "1" "1" "1" "0" ...
#>  $ gear: chr [1:32] "4" "4" "4" "3" ...
#>  $ carb: chr [1:32] "4" "4" "1" "1" ...
#>  - attr(*, "spec")=
#>   .. cols(
#>   ..   .default = col_character(),
#>   ..   mpg = col_character(),
#>   ..   cyl = col_character(),
#>   ..   disp = col_character(),
#>   ..   hp = col_character(),
#>   ..   drat = col_character(),
#>   ..   wt = col_character(),
#>   ..   qsec = col_character(),
#>   ..   vs = col_character(),
#>   ..   am = col_character(),
#>   ..   gear = col_character(),
#>   ..   carb = col_character()
#>   .. )

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

As you can see from above it is not ignored, could you show a more complete example, perhaps there is something else going on.

Yep apologies, embarrassing typo. Sometimes you need to step away for a bit.

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.