The The R Primer logo Primer

Read data from a CSV file

You want to import a dataset stored as a comma-separated values (CSV) file

Solution:

A CSV file is a plain text file where each line corresponds to a case and where the case variables are separated by commas. Quotation marks are used to embed field values that contain the separator character.

Use read.csv to read in the delimited file just like you would use \defineRfunction{read.table}. Actually, read.csv is a wrapper function that sets the correct options for read.table. read.csv2 is used to read in semicolon separated files which is the default CSV-format in some locales where comma is the decimal point character and where semicolon is used as separator. To read the CSV file mydata.csv that looks as follows:


"Id","Sex","Age","Score"
21,"Male",14,"Little"
26,"Male",13,"None"
27,"Male",13,"Moderate, severe"
29,"Female",13,"Little"
30,"Female",15,"Little"
31,"Male",14,"Moderate, severe"
we use the command

> indata <- read.csv("mydata.csv")
> head(indata)
  Id    Sex Age            Score
1 21   Male  14           Little
2 26   Male  13             None
3 27   Male  13 Moderate, severe
4 29 Female  13           Little
5 30 Female  15           Little
6 31   Male  14 Moderate, severe
Both read.csv and read.csv2 assume that a header line is present in the CSV file (ie, header=TRUE is the default). If no header line is present you need to specify the header=FALSE argument.

See rule 1.5 in The R Primer.

Back to tips.