CHAPTER 12
Beginner
File Handling in R
Updated: May 18, 2026
5 min read
# CHAPTER 12
File Handling in R
1. Chapter Introduction
Data analysis always involves reading from and writing to files. This chapter covers R's complete file handling toolkit — text files, CSVs, binary files, and directory management — essential for building reproducible data pipelines.2. Reading Files
r
3. Writing Files
r
4. File System Operations
r
5. Mini Project: Notes Management System
r
6. Common Mistakes
-
write.csv()includes row names by default: This adds an unwantedXcolumn when you re-read the CSV. Always userow.names=FALSE.
-
Relative vs absolute paths:
setwd()changes directory, making relative paths fragile. Use R Projects +here::here()for reproducible paths.
7. MCQs
Question 1
read.csv("file.csv", row.names=1) uses column 1 as?
Question 2
saveRDS(obj, "file.rds") saves?
Question 3
write.csv() default includes row names?
Question 4
list.files(pattern="\\.csv$") returns?
Question 5
file.exists() returns?
Question 6
append=TRUE in cat(file=...) does?
Question 7
readLines() reads file as?
Question 8
dir.create(recursive=TRUE) creates?
Question 9
readcsv() from readr vs read.csv()?
Question 10
load("workspace.RData") restores?
8. Interview Questions
-
Q: What is the difference between
saveRDS()andsave()in R?
-
Q: Why should you use
row.names=FALSEwithwrite.csv()?
9. Summary
File reading:read.csv() (base), readcsv() (readr, faster). Always specify stringsAsFactors=FALSE in older R. Writing: write.csv(row.names=FALSE), writecsv(). Binary R objects: saveRDS()/readRDS() (single object), save()/load() (multiple). File system: file.exists(), dir.create(recursive=TRUE), list.files(pattern=). Use R Projects + here package for portable paths.