Reading Data (CSV)
Reading data is how you get real datasets into R — read.csv() turns a comma-separated file into a data frame you can explore, clean, and analyse.
Learn Reading Data (CSV) in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll load CSV files (and inline CSV text) into data frames, check the result with str(), control the separator and missing-value handling, and work with NA values safely.
What You'll Learn in This Lesson
1️⃣ Reading a CSV File
read.csv("file.csv") reads the file into a data frame, using the first row as column names. The path is relative to your working directory. Always follow up with str() to confirm the types.
2️⃣ Reading Inline CSV Text
For quick tests (and for this course), you can pass CSV text directly with text = . It behaves exactly like reading a file, so it's perfect for experimenting without a file on disk.
3️⃣ Separators and Missing Values
Real files aren't always clean. Use sep for non-comma separators and na.strings to mark placeholders as missing. Once data has NA , statistics need na.rm = TRUE to skip them.
Your turn. Fill in the # TODO blank, run it, and compare with the expected output.
Mini-Challenge: Missing Temperatures
Write it from the outline, run it, and check it against the example output. Handling that one NA correctly is the whole point.
Common Errors (and the fix)
- "cannot open file ...: No such file or directory" — the path is wrong or the working directory isn't where the file lives. Check getwd() .
- Numbers read as text — a stray symbol or comma in the column. Clean it or use na.strings , then convert with as.numeric() .
- mean() returns NA — there are missing values. Add na.rm = TRUE to skip them.
- Everything in one column — the file uses a different separator. Set sep = ";" or sep = "\\t" .
Pro Tips
- 💡 Inspect immediately: str() and summary() right after loading catch most data problems.
- 💡 Read straight from a URL: read.csv("https://...") works for public CSV links.
- 💡 Faster for big files: readr::read_csv() and data.table::fread() handle large data quickly.
📋 Quick Reference — Reading Data
Task
R Syntax
Read a CSV file
read.csv("f.csv")
Read CSV text
read.csv(text = s)
Other separator
read.csv(f, sep = ";")
Mark missing
na.strings = "N/A"
Count missing
sum(is.na(x))
Stats ignoring NA
mean(x, na.rm = TRUE)
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ read.csv() turns a CSV into a data frame
- ✅ text = reads inline CSV for quick tests
- ✅ Always str() after loading to check types
- ✅ sep and na.strings handle messy files
- ✅ na.rm = TRUE computes stats despite missing data
- ✅ Next lesson: The apply Family — functions over rows, cols, and lists
Practice quiz
Which function reads a CSV file into a data frame?
- read.table.only()
- read.csv()
- load.csv()
- open.csv()
Answer: read.csv(). read.csv() loads a comma-separated file into a data frame.
How do you read CSV from a text string instead of a file?
- read.csv(file = s)
- read.csv(string = s)
- read.csv(text = s)
- read.csv(s, inline = TRUE)
Answer: read.csv(text = s). Passing text = supplies CSV content directly, no file needed.
What does the header argument default to in read.csv()?
- FALSE
- TRUE
- NULL
- "auto"
Answer: TRUE. header defaults to TRUE, treating the first row as column names.
Which argument changes the field separator to a semicolon?
- delim = ";"
- split = ";"
- field = ";"
- sep = ";"
Answer: sep = ";". sep = ";" tells read.csv() to split on semicolons.
What does NA represent in R?
- A missing (not available) value
- The number zero
- An empty string
- A syntax error
Answer: A missing (not available) value. NA marks a missing value; calculations on it return NA by default.
What does mean(x) return when x contains an NA?
- The mean of non-NA values
- Zero
- NA
- An error
Answer: NA. By design NA spreads, so mean() returns NA unless told otherwise.
How do you compute the mean while skipping missing values?
- mean(x, skip = TRUE)
- mean(x, ignore = NA)
- mean(na.omit)
- mean(x, na.rm = TRUE)
Answer: mean(x, na.rm = TRUE). na.rm = TRUE removes NA values before computing.
Which argument marks specific text as missing while reading?
- na.strings
- missing
- fill
- blank.lines
Answer: na.strings. na.strings lists the tokens that should be read as NA.
How do you count the missing values in a vector x?
- count.na(x)
- length(NA)
- sum(is.na(x))
- nrow(x)
Answer: sum(is.na(x)). is.na(x) gives a logical vector; sum() counts the TRUEs.
Why run str() right after reading a CSV?
- To delete the file
- To confirm the columns came in with the expected types
- To plot the data
- To rename the file
Answer: To confirm the columns came in with the expected types. str() reveals each column's type so you catch numbers read as text early.
Continue this course
- Previous: Data Frames
- Next: The apply Family