Reading Data into R
Sooner or later you stop using built-in datasets and load your own. This is how. Everything below is a live R console — edit the code and press Run Code.
Watch: reading data into R
The working directory, read.csv() and write.csv(), and why you should always inspect a fresh import.
Download the R script · ▶ Practice in the playground — or edit and run each snippet below.
Where does R look for files?
R reads and writes relative to the working directory. getwd() shows it (setwd() changes it):
Write a CSV, then read it back
write.csv() saves a data frame; read.csv() loads one. Here we round-trip mtcars:
Always look after importing
Before analysing, check the structure and the first rows — column types and unexpected text hide here:
Real files and faster reads
For a file on disk you give the path, e.g. read.csv("data/cars.csv"). For big files the readr package’s read_csv() is faster and guesses types better:
Your turn
Next: Writing your own functions → — packaging steps you repeat.