01 / 20

Reading CSV Files
in R

Learn to import comma-separated data into R using read.csv() and readr::read_csv(): the essential first step in any data analysis workflow.

R Programming

What Is a CSV File?

A CSV (Comma-Separated Values) file is a plain text file where each line represents a row of data and each value within that row is separated by a comma. It is the most common format for sharing tabular data.

RAW TEXT name,age,grade Alice,16,A Bob,17,B Carol,16,A Dave,18,C commas = delimiters R DATA FRAME name age grade Alice 16 A Bob 17 B Carol 16 A Dave 18 C

Why CSV Matters

★ Universal
Every spreadsheet app, database, and programming language can read and write CSV files.
★ Lightweight
No formatting overhead: just raw data and commas. Tiny file sizes compared to Excel.
★ Transparent
Open in any text editor to inspect the data: no hidden formulas or macros.

Anatomy of a CSV File

1
Header row (optional): The first line often contains column names, separated by commas.
2
Data rows: Each subsequent line is one observation. Values are separated by commas in the same order as the header.
3
Delimiter: The comma is the default separator, but some files use semicolons, tabs, or pipes instead.
4
Quoting: If a value itself contains a comma, it is wrapped in double quotes: "New York, NY".

The Core Function: read.csv()

read.csv() is built into base R: no packages needed. It reads a CSV file and returns a data.frame.

# Basic usage df <- read.csv("students.csv")
file
Path to your CSV file (relative or absolute)
header
TRUE by default: first row is treated as column names
stringsAsFactors
FALSE in R 4.0+: text stays as character, not factor

The Modern Alternative: read_csv()

readr::read_csv() from the tidyverse is faster, gives cleaner output, and returns a tibble instead of a data.frame.

# Install once, then load library(readr) df <- read_csv("students.csv")

read.csv() vs. read_csv()

read.csv() read_csv()
Package Base R (built-in) readr (tidyverse)
Returns data.frame tibble
Speed Slower on large files Faster (C++ engine)
Strings Factor (\( R < 4.0 \)) Always character
Column specs None shown Prints summary
Encoding System default UTF-8 always

Key Parameters You Should Know

1
file: Path to your CSV. Can be a local path or a URL.
2
header / col_names: Whether the first row is column names. TRUE by default in both functions.
3
sep / delim: The delimiter character. read.csv() uses sep = ","; read_csv() auto-detects commas.
4
na.strings / na: What values should be treated as missing (NA)? Default: "" and "NA".
5
skip: Number of lines to skip before reading. Useful when the file has metadata rows at the top.

How to Read a CSV Step by Step

1
Place the file: Put your CSV in your working directory, or note its full path.
2
Check your working directory: Run getwd() to confirm R is looking in the right folder.
3
Read the file: Call read.csv("file.csv") or read_csv("file.csv") and assign it to a variable.
4
Inspect the result: Use head(df), str(df), or glimpse(df) to verify columns and types.
5
Fix issues: Adjust parameters like skip, na.strings, or col_types if needed.

Example 1: Basic CSV Read

R CODE
# Read a local CSV file students <- read.csv("students.csv") # View first few rows head(students)
OUTPUT
name age grade 1 Alice 16 A 2 Bob 17 B 3 Carol 16 A 4 Dave 18 C

Example 2: Custom Parameters

Suppose your CSV uses semicolons as delimiters, has 2 metadata lines at the top, and uses "N/A" for missing values.

# Handling a tricky CSV data <- read.csv( "messy_data.csv", sep = ";", skip = 2, na.strings = c("", "NA", "N/A") )

Example 3: Using readr::read_csv()

library(readr) students <- read_csv("students.csv")
CONSOLE OUTPUT
Rows: 4 Columns: 3 -- Column specification ----------- Delimiter: "," chr (2): name, grade dbl (1): age i Use `spec()` to retrieve the full column specification.

Reading from a URL

Both functions accept a URL instead of a local file path. R will download the file automatically.

url <- "https://example.com/data.csv" # Base R df1 <- read.csv(url) # Tidyverse df2 <- read_csv(url)

Inspecting Your Data

After reading a CSV, always inspect the result to confirm it loaded correctly.

head(df)
Shows the first 6 rows: quick sanity check.
str(df)
Displays structure: column names, types, and sample values.
nrow(df)
Returns the number of rows: does it match your expectations?
summary(df)
Statistical summary of each column: min, max, mean, NAs.

Common Mistakes

✕ Wrong File Path
R cannot find the file because your working directory is different from where the file lives. Always check with getwd().
✕ Mixing Up the Functions
Using read_csv() without loading the readr package first, or confusing the dot (read.csv) with the underscore (read_csv).
✕ Wrong Delimiter
File uses semicolons but you called read.csv() without sep = ";": all data collapses into one column.

Question 1 of 5

Which function is built into base R and requires no additional packages to read a CSV file?

Question 2 of 5

What type of object does readr::read_csv() return by default?

Question 3 of 5

Your CSV file has 3 lines of metadata at the top before the actual data starts. Which parameter lets you skip those lines?

Question 4 of 5

You run read.csv("data.csv") and the resulting data frame has only 1 column with values like "Alice;16;A". What is the most likely problem?

Question 5 of 5

After loading a CSV, which R function gives you a quick look at the first 6 rows of your data?