Reading data into R — what to watch out for
read.table() can do (almost) everything
If you have RStudio installed, you already have the read.table() function. It can read almost any dataset, though there are packages and other functions that make this easier. Let’s start with read.table(). As an aside, in RStudio you can always look up documentation for any function under the “Help” tab — just type in the function name, which works for read.table() too.
The documentation shows that alongside read.table(), the same package also includes read.csv(), read.csv2(), read.delim(), and read.delim2(). So what’s the difference?
help(read.table)
As mentioned, these related functions make life easier: while read.table() requires us to define every argument ourselves, the others come with sensible defaults already set. So which conditions apply, and when do you use which function?
Conditions and differences between the read.* functions
First, some basics. To import data at all, you need to tell read.table() where to find it. There are two options. Either you’ve already set a working directory via a new project (highly recommended!) — then you just drop the data into that folder and don’t need to specify a path. Or, if there’s no working directory yet, you can set one directly:
path <- "C:/Users/.../data" # the path to the folder
setwd(path) # sets it
Excerpt from the table we want to read in
(The original post included a screenshot of the raw data file here; it wasn’t carried over when this site was rebuilt.)
We can spot the following:
- The first three lines are irrelevant and need to go, since R always takes the first line as the header.
- Line 5 is empty.
- The columns are separated by spaces.
- The numbers have no decimal comma or point (so technically we don’t need to define one).
- And something you can’t see here, but it’s in the file: the header repeats halfway through (line 491) — the recording software made a mistake there. That row must not be imported.
As you can see, there’s a fair bit to handle before this file imports correctly. The table below shows the available arguments — some you can leave as-is, others you have to set explicitly for the import to work.
| Argument | What it does |
|---|---|
header |
Whether the first row holds column names |
skip |
Number of lines to skip before reading begins |
sep |
The field separator, e.g. "\t" for tab, " " for space |
dec |
The decimal separator, "." or "," |
fill |
Pads rows with too few fields with NA instead of failing |
blank.lines.skip |
Whether empty lines are skipped |
nrows |
Maximum number of rows to read |
Now we can write our function:
data <- read.table("Proband1-CPT.txt",
header = TRUE,
skip = 3,
sep = "\t",
dec = ",",
fill = TRUE,
blank.lines.skip = TRUE,
nrows = 488) # read everything up to line 488
There’s a faster way
The functions read.csv(), read.csv2(), read.delim(), and read.delim2() can save you a lot of work — most arguments already come with sensible defaults, and you only need to specify the exceptions.
read.csv(file, header = TRUE, sep = ",", dec = ".", fill = TRUE, ...)
read.csv2(file, header = TRUE, sep = ";", dec = ".", fill = TRUE, ...)
read.delim(file, header = TRUE, sep = "\t", dec = ".", fill = TRUE, ...)
read.delim2(file, header = TRUE, sep = "\t", dec = ";", fill = TRUE, ...)
As you can see, we only need read.delim2() here. This function is built for data that uses spaces as separators and decimal commas. Our code gets shorter:
data <- read.delim2("Proband1-CPT.txt",
skip = 3,
nrows = 488) # read everything up to line 488
The difference between read.csv() and read.delim() on one hand, and read.csv2() and read.delim2() on the other, is simple in principle: the “2” variants are built for us Europeans, since we separate numbers with a comma rather than a point, and typically use a semicolon rather than a plain comma between values.