install.packages("tidyverse")
Import and Export Data with readr
Slides in full screen Download PDF slides
1 Task
Find the solution here.
1.1 Get started with readr and the tidyverse
Before you start, make sure to install the tidyverse
packages by calling
This will install readr along with other tidyverse packages.
Remember to put library(tidyverse)
(or library(readr)
) on top of your script to access the readr functions.
1.1.1 Write a tibble to disk
Let’s use the animals
tibble from the previous task and write it into the data folder in our project.
Before writing the tibble
- Create a
data
sub-folder in your RStudio project (if you don’t have one yet)- Hint: You can do that from within RStudio by using the
New Folder
button in the Files pane
- Hint: You can do that from within RStudio by using the
Now write the animals
tibble into that /data
sub-directory as animals.csv
using a comma separator.
Check if the file was written into the correct folder.
1.2 Read data into R
Now, try to read the data set back into R using the appropriate read_*
function.
Make sure that you save the table you read in in a new variable to have it available for later use.
Don’t type the path of the table to read. Instead, type only the ““, then us the tab key on your keyboard to auto-complete the path. This way you avoid typing mistakes.
1.3 For the fast ones
- Download the csv or excel file below. The files are a bit messy. Find out what is messy about the data sets (try reading them in R and see what happens, open the files in Excel or a text editor and check out the structure). Then read them into R correctly. Try the
janitor::clean_names
function on the data after you read it in.
- Try reading in some of your own research data. First, add them to the
/data
folder of your project. Then read them into R using the appropriateread_*
function. Can you manage to read the data in a clean format? What are the challenges you face?