Get startedGet started for free

readr: read_csv & read_tsv

1. readr: read_csv & read_tsv

By now, you already now how to import flat files using

2. Overview

the utils package. The utils package is loaded by default when you start R. Of course, R wouldn't be R if there weren't specialized packages to import your data. In this chapter, I'm going to talk about two such packages: readr and data table.

3. readr

First, let's dive into the "readr" package, written by Hadley Wickham! It's a fast and very easy to use package with very consistent naming, while utils is more verbose and multiple times slower. We'll start with installing and loading the readr package, like this.

4. CSV files

Before, you used read dot csv to import CSV files as a data frame, like this call, to import the states dot csv file. To do this the readr way, you'll want to use read underscore csv. This will do the trick: The result is pretty much the same. The only difference is that read_delim outputs a tibble, which is a supercharged version of a data frame that Hadley Wickham introduced; you can work with it as a normal data frame, but can do additional stuff with it. The printout conveniently shows the column classes. In general, all of Hadley's packages work with tibbles; whether or not they show this convienent printout depends on the packages you have loaded into your R session. In both calls, the first argument is the path to the file you want to import. Remember that utils also featured the read-dot-delim function to import tab-delimited files.

5. TSV files

This call imported states-dot-txt. readr also provides a similar function, but it's called read underscore tsv, short for tab separated value. This is the call you need.

6. Wrapping in utils and readr

Just like in utils, both the read_csv and read_tsv functions are wrappers around a 'mother import function',

7. Wrapping in utils and readr

called read_delim. This table summarizes how the wrapping works for utils and readr, make sure not to mix things up.

8. Let's practice!

I suggest you already head over to the exercises for some practice before we dive into some more customization possibilities for read_delim. I'll see you again in the next video!