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

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any R Language Question?