Reading CSV Files
In this page:
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
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")
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: