This is useful if you need to do some manual munging - you can read the columns in as character, clean it up with (e.g.) regular expressions and then let readr take another stab at parsing it. The name is a homage to the base type.convert().

type_convert(df, col_types = NULL, na = c("", "NA"), trim_ws = TRUE,
  locale = default_locale())

Arguments

df

A data frame.

col_types

One of NULL, a cols() specification, or a string. See vignette("column-types") for more details.

If NULL, all column types will be imputed from the first 1000 rows on the input. This is convenient (and fast), but not robust. If the imputation fails, you'll need to supply the correct types yourself.

If a column specification created by cols(), it must contain one column specification for each column. If you only want to read a subset of the columns, use cols_only().

Unlike other functions type_convert() does not allow character specifications of col_types.

na

Character vector of strings to use for missing values. Set this option to character() to indicate no missing values.

trim_ws

Should leading and trailing whitespace be trimmed from each field before parsing it?

locale

The locale controls defaults that vary from place to place. The default locale is US-centric (like R), but you can use locale() to create your own locale that controls things like the default time zone, encoding, decimal mark, big mark, and day/month names.

Examples

df <- data.frame( x = as.character(runif(10)), y = as.character(sample(10)), stringsAsFactors = FALSE ) str(df)
#> 'data.frame': 10 obs. of 2 variables: #> $ x: chr "0.0807501375675201" "0.834333037259057" "0.600760886212811" "0.157208441523835" ... #> $ y: chr "9" "2" "1" "3" ...
str(type_convert(df))
#> Parsed with column specification: #> cols( #> x = col_double(), #> y = col_integer() #> )
#> 'data.frame': 10 obs. of 2 variables: #> $ x: num 0.0808 0.8343 0.6008 0.1572 0.0074 ... #> $ y: int 9 2 1 3 7 8 10 5 6 4
df <- data.frame(x = c("NA", "10"), stringsAsFactors = FALSE) str(type_convert(df))
#> Parsed with column specification: #> cols( #> x = col_integer() #> )
#> 'data.frame': 2 obs. of 1 variable: #> $ x: int NA 10
# Type convert can be used to infer types from an entire dataset type_convert( read_csv(readr_example("mtcars.csv"), col_types = cols(.default = col_character())))
#> 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() #> )
#> # A tibble: 32 × 11 #> mpg cyl disp hp drat wt qsec vs am gear carb #> <dbl> <int> <dbl> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int> #> 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 #> 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 #> 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 #> 4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 #> 5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 #> 6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 #> 7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 #> 8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 #> 9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 #> 10 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 #> # ... with 22 more rows