1.4 Loading a Delimited Text Data File

1.4.1 Problem

You want to load data from a delimited text file.

1.4.2 Solution

The most common way to read in a file is to use comma-separated values (CSV) data:

Alternatively, you can use the read_csv() function (note the underscore instead of period) from the readr package. This function is significantly faster than read.csv(), and

1.4.3 Discussion

Since data files have many different formats, there are many options for loading them. For example, if the data file does not have headers in the first row:

The resulting data frame will have columns named V1, V2, and so on, and you will probably want to rename them manually:

You can set the delimiter with sep. If it is space-delimited, use sep = " ". If it is tab-delimited, use \t, as in:

By default, strings in the data are treated as factors. Suppose this is your data file, and you read it in using read.csv():

"First","Last","Sex","Number"
"Currer","Bell","F",2
"Dr.","Seuss","M",49
"","Student",NA,21

The resulting data frame will store First and Last as factors, though it makes more sense in this case to treat them as strings (or character vectors in R terminology). To differentiate this, use stringsAsFactors = FALSE. If there are any columns that should be treated as factors, you can then convert them individually:

Alternatively, you could load the file with strings as factors, and then convert individual columns from factors to characters.

1.4.4 See Also

read.csv() is a convenience wrapper function around read.table(). If you need more control over the input, see ?read.table.