readr (version 0.2.2)

type_convert: Re-convert character columns in existing data frame.

Description

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.

Usage

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 of 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 "collector" for each column. If you only want to read a subset of the columns, use cols_only.

Alternatively, you can use a compact string representation where each character represents one column: c = character, i = integer, n = number, d = double, l = logical, D = date, T = date time, t = time, ? = guess, or _/- to skip the column.

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

Run this code
df <- data.frame(
  x = as.character(runif(10)),
  y = as.character(sample(10)),
  stringsAsFactors = FALSE
)
str(df)
str(type_convert(df))

df <- data.frame(x = c("NA", "10"), stringsAsFactors = FALSE)
str(type_convert(df))

Run the code above in your browser using DataLab