Load packages
library(tidyverse)
Motivation
Importing data to R can cause headaches for newbies. For some, the concept of relative and absolute paths is new. That’s why I compiled here some recommendations on how to important data into R and on how to ditch the “what’s my path” problem.
Approach 1: Start an RStudio project
That’s an approach I generally recommend.
- Start an RStudio project.
- Put your code files and your data files in this very folder.
- Import the data without specifying any paths, eg.,
d <- read.csv("mydata.csv)
Approach 2: Import from an online source
That’s also convenient – as long as someone has put the data online.
- Identify the URL of your data set, and copy it.
- Open up an R/Rmd script file and paste the URL to your import function, e.g,.
d <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/reshape2/tips.csv")
Approach 3: Learn what a path means
- Identify the location (path) of your data file on your computer.
- Copy it to the clipboard (depends on your operating system).
- Paste it into your import function, e.g, `d <- read.csv(“/Users/sebastiansaueruser/datasets/tips.csv”)
Example time – dataset tips
tips <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/reshape2/tips.csv")