5.4 Import a local CSV file

You can find additional help on importing different types of files at the Data Import Cheat Sheet.

Steps to import a local CSV file

We’ll use the “trout.csv” file that we created previously.

And we’ll make use of the here package that we were introduced to in an earlier tutorial.

Let’s load the package:


New tool: Pipes or “%>%” are implemented as part of the magrittr package, which is loaded with the tidyverse. In brief, pipes allow us to string together a series of functions, and we’ll use them frequently in tutorials.

Here we’ll use a pipe to help import the data file.

Let’s see the code first, then explain after:

trout <- here("rawdata", "trout.csv") %>%
  • First we have the name of the object (a “tibble”) that we wish to create, “trout”.
  • Then you see the assignment operator “<-”, which tells R to assign whatever we’re doing to the right of the operator to the object “trout”.
  • Then we have the here function, which is taking two inputs: the name of the directory we wish to get something from (“rawdata”), and then the name of the file we wish to do something with, here “trout.csv”).
  • Then we have a pipe “%>%”, which tells R that we’re not done coding yet - there’s more to come on the next line…
  • Lastly, we use the read_csv function, whatever came before the pipe is what is fed to the read_csv function.

Go ahead and run the chunk of code above to create the “trout” object.

Next we’ll learn how to get an overview of the data stored in a tibble object.