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.
Write it from the outline, run it, and check it against the example output. Handling that one NA correctly is the whole point.
📋 Quick Reference — Reading Data
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.