Reading and writing tabular data in plain-text files (CSV, TSV, etc.)

Download r eBook

Syntax

  • read.csv(file, header = TRUE, sep = ",", quote = """, dec = ".", fill = TRUE, comment.char = "", ...)

  • read.csv2(file, header = TRUE, sep = ";", quote = """, dec = ",", fill = TRUE, comment.char = "", ...)

  • readr::read_csv(file, col_names = TRUE, col_types = NULL, locale = default_locale(), na = c("", "NA"), comment = "", trim_ws = TRUE, skip = 0, n_max = -1, progress = interactive())

  • data.table::fread(input, sep="auto", sep2="auto", nrows=-1L, header="auto", na.strings="NA", stringsAsFactors=FALSE, verbose=getOption("datatable.verbose"), autostart=1L, skip=0L, select=NULL, drop=NULL, colClasses=NULL, integer64=getOption("datatable.integer64"), # default: "integer64" dec=if (sep!=".") "." else ",", col.names, check.names=FALSE, encoding="unknown", strip.white=TRUE, showProgress=getOption("datatable.showProgress"), # default: TRUE data.table=getOption("datatable.fread.datatable") # default: TRUE )

Parameters

ParameterDetails
filename of the CSV file to read
headerlogical: does the .csv file contain a header row with column names?
sepcharacter: symbol that separates the cells on each row
quotecharacter: symbol used to quote character strings
deccharacter: symbol used as decimal separator
filllogical: when TRUE, rows with unequal length are filled with blank fields.
comment.charcharacter: character used as comment in the csv file. Lines preceded by this character are ignored.
...extra arguments to be passed to read.table

Remarks

Note that exporting to a plain text format sacrifices much of the information encoded in the data like variable classes for the sake of wide portability. For cases that do not require such portability, a format like .RData or Feather may be more useful.

Input/output for other types of files is covered in several other topics, all linked from Input and output.

Related Examples

Stats

676 Contributors: 47
Monday, June 12, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook