← Back to R Course | Chapter 8: Data Manipulation | Lesson 1 of 7

Reading CSV Files

In this page:

  1. Reading CSV Files

Reading CSV Files

read.csv("file.csv") reads a comma-separated file directly into a data frame, automatically inferring column types from the data. It's the most common way to load tabular data into R for analysis. header = TRUE (the default) treats the first row as column names.

Note: Pass stringsAsFactors = FALSE (the modern default in recent R versions) if you want text columns to stay as character rather than becoming factors.

Example: Reading CSV Files

markup
csv_text <- "name,age\nAlice,30\nBob,25"
writeLines(csv_text, "people.csv")

df <- read.csv("people.csv")
print(df)
cat(class(df$age), "\n")
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.