Learn R Programming

readr (version 0.1.1)

read_delim: Read a delimited file into a data frame.

Description

read_csv and read_tsv are special cases of the general read_delim. They're useful for reading the most common types of flat file data. read_csv2 uses ; for separators, instead of ,. This is common in European countries which use , as the decimal separator.

Usage

read_delim(file, delim, quote = '"', escape_backslash = TRUE,
  escape_double = FALSE, na = "NA", col_names = TRUE, col_types = NULL,
  skip = 0, n_max = -1, progress = interactive())

read_csv(file, col_names = TRUE, col_types = NULL, na = "NA", skip = 0, n_max = -1, progress = interactive())

read_csv2(file, col_names = TRUE, col_types = NULL, na = "NA", skip = 0, n_max = -1, progress = interactive())

read_tsv(file, col_names = TRUE, col_types = NULL, na = "NA", skip = 0, n_max = -1, progress = interactive())

Arguments

file
Either a path to a file, a connection, or literal data (either a single string or a raw vector).

Files ending in .gz, .bz2, .xz, or .zip will be automatically uncompressed. Files starting with

delim
Single character used to separate fields within a record.
quote
Single character used to quote strings.
escape_backslash
Does the file use backslashes to escape special characters? This is more general than escape_double as backslashes can be used to escape the delimeter character, the quote characer, or to add special characters like \n.
escape_double
Does the file escape quotes by doubling them? i.e. If this option is TRUE, the value """" represents a single quote, ".
na
String to use for missing values.
col_names
Either TRUE, FALSE or a character vector of column names.

If TRUE, the first row of the input will be used as the column names, and will not be included in the data frame. If FALSE, column names

col_types
One of NULL, a list, a named list or a string.

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

skip
Number of lines to skip before reading data.
n_max
Maximum number of records to read.
progress
Display a progress bar? By default it will only display in an interactive session. The display is updated every 50,000 values and will only display if estimated reading time is 5 seconds or more.

Value

  • A data frame. If there are parsing problems, a warning tells you how many, and you can retrieve the details with problems().

Examples

Run this code
# Input sources -------------------------------------------------------------
# Read from a path
read_csv(system.file("extdata/mtcars.csv", package = "readr"))
read_csv(system.file("extdata/mtcars.csv.zip", package = "readr"))
read_csv(system.file("extdata/mtcars.csv.bz2", package = "readr"))
read_csv("https://github.com/hadley/readr/raw/master/inst/extdata/mtcars.csv")

# Or directly from a string (must contain a newline)
read_csv("x,y\n1,2\n3,4")

# Column types --------------------------------------------------------------
# By default, readr guess the columns types, looking at the first 100 rows.
# You can override with a compact specification:
read_csv("x,y\n1,2\n3,4", col_types = "dc")

# Or with a list of column types:
read_csv("x,y\n1,2\n3,4", col_types = list(col_double(), col_character()))

# If there are parsing problems, you get a warning, and can extract
# more details with problems()
y <- read_csv("x\n1\n2\nb", col_types = list(col_double()))
y
problems(y)

# File types ----------------------------------------------------------------
read_csv("a,b\n1.0,2.0")
read_csv2("a;b\n1,0;2,0")
read_tsv("a\tb\n1.0\t2.0")
read_delim("a|b\n1.0|2.0", delim = "|")

Run the code above in your browser using DataLab