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("readr") for more details.

If NULL, column types will be imputed using all rows.

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_double() #> )
#> 'data.frame': 10 obs. of 2 variables: #> $ x: num 0.0808 0.8343 0.6008 0.1572 0.0074 ... #> $ y: num 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_double() #> )
#> 'data.frame': 2 obs. of 1 variable: #> $ x: num 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_double(), #> disp = col_double(), #> hp = col_double(), #> drat = col_double(), #> wt = col_double(), #> qsec = col_double(), #> vs = col_double(), #> am = col_double(), #> gear = col_double(), #> carb = col_double() #> )
#> # A tibble: 32 x 11 #> mpg cyl disp hp drat wt qsec vs am gear carb #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 21.0 6.00 160 110 3.90 2.62 16.5 0 1.00 4.00 4.00 #> 2 21.0 6.00 160 110 3.90 2.88 17.0 0 1.00 4.00 4.00 #> 3 22.8 4.00 108 93.0 3.85 2.32 18.6 1.00 1.00 4.00 1.00 #> 4 21.4 6.00 258 110 3.08 3.22 19.4 1.00 0 3.00 1.00 #> 5 18.7 8.00 360 175 3.15 3.44 17.0 0 0 3.00 2.00 #> 6 18.1 6.00 225 105 2.76 3.46 20.2 1.00 0 3.00 1.00 #> 7 14.3 8.00 360 245 3.21 3.57 15.8 0 0 3.00 4.00 #> 8 24.4 4.00 147 62.0 3.69 3.19 20.0 1.00 0 4.00 2.00 #> 9 22.8 4.00 141 95.0 3.92 3.15 22.9 1.00 0 4.00 2.00 #> 10 19.2 6.00 168 123 3.92 3.44 18.3 1.00 0 4.00 4.00 #> # ... with 22 more rows