File and system operations are essential for data analysis workflows, allowing you to manage input/output files, organize project directories, and interact with the operating system. R provides robust functions for checking file existence, creating directories, manipulating files, and working with various file formats including its native RDA format.

This comprehensive guide covers all file and system operations with practical examples.

Checking File and Directory Existence

Check if File Exists

# Check if file exists
file.exists("data.csv")           # TRUE or FALSE
file.exists("nonexistent.txt")    # FALSE

# Check multiple files
files <- c("data1.csv", "data2.csv", "data3.csv")
file.exists(files)

# Get full path
file_path <- "C:/Users/Documents/data.csv"
file.exists(file_path)

Check if Directory Exists

# Check if directory exists
dir.exists("data")                # TRUE or FALSE
dir.exists("/path/to/directory")  # TRUE or FALSE
dir.exists("nonexistent_dir")     # FALSE

# Get current working directory
getwd()

# Set working directory
setwd("C:/Users/Documents/R")
getwd()  # Verify change

Check File Status and Properties

# Get file information
file_info <- file.info("data.csv")
print(file_info)
#            size isdir mode               mtime               ctime               atime
# data.csv 12345 FALSE  644 2023-11-15 10:30:00 2023-11-15 10:30:00 2023-11-20 14:22:15

# Extract specific information
file_info$size          # File size in bytes
file_info$isdir         # Is it a directory?
file_info$mtime         # Last modification time

# File size in MB
file_size_mb <- file_info$size / (1024^2)

File Operations

List Files in Directory

# List all files in current directory
list.files()

# List files with pattern matching
list.files(pattern = ".csv$")    # Only CSV files
list.files(pattern = "^data")    # Files starting with "data"

# List with full paths
list.files(full.names = TRUE)

# List directories only
list.dirs()

# Recursive listing
list.files(recursive = TRUE)     # All files in subdirectories

# Count files
length(list.files())

Delete Files

# Delete a single file
file.remove("old_data.csv")

# Delete multiple files
files_to_delete <- c("temp1.txt", "temp2.txt", "temp3.txt")
file.remove(files_to_delete)

# Delete and check success
success <- file.remove("unwanted.csv")
if (success) {
  print("File deleted successfully")
} else {
  print("File deletion failed")
}

Rename Files

# Rename single file
file.rename(from = "old_name.csv", to = "new_name.csv")

# Rename with path
file.rename(from = "data/raw.csv", to = "data/processed.csv")

# Batch rename
old_names <- c("file1.txt", "file2.txt", "file3.txt")
new_names <- c("document1.txt", "document2.txt", "document3.txt")
file.rename(from = old_names, to = new_names)

# Rename with timestamp
Sys.time()
timestamp <- format(Sys.time(), "%Y%m%d_%H%M%S")
new_filename <- paste0("data_", timestamp, ".csv")
file.rename("data.csv", new_filename)

Copy Files

# Copy single file
file.copy(from = "original.csv", to = "backup.csv")

# Copy with overwrite
file.copy(from = "original.csv", to = "backup.csv", overwrite = TRUE)

# Copy to directory
file.copy(from = "data.csv", to = "archive/")

# Copy multiple files
files <- c("file1.csv", "file2.csv", "file3.csv")
file.copy(from = files, to = "backup/")

Directory Operations

Create Directories

# Create single directory
dir.create("data")

# Create with full path
dir.create("data/raw", recursive = FALSE)

# Create nested directories
dir.create("data/raw/2023", recursive = TRUE)

# Create only if doesn't exist
if (!dir.exists("output")) {
  dir.create("output")
}

Remove Directories

# Remove empty directory
unlink("temp_dir", recursive = FALSE)

# Remove directory with contents
unlink("backup_dir", recursive = TRUE)

# Conditional removal
if (dir.exists("old_data")) {
  unlink("old_data", recursive = TRUE)
}

Working with RDA Files

RDA (R Data Archive) is R’s native binary format for saving objects.

Save Objects to RDA

# Save single object
data <- data.frame(x = 1:10, y = rnorm(10))
save(data, file = "mydata.rda")

# Save multiple objects
model <- lm(y ~ x, data = data)
results <- summary(model)
save(data, model, results, file = "analysis.rda")

# Save with compression
save(data, file = "mydata.rda", compress = "gzip")

# Save entire workspace
save.image(file = "workspace.rda")

Load RDA Files

# Load single RDA file
load("mydata.rda")

# Load specific file into environment
load("mydata.rda", envir = new.env())

# Check what's in RDA before loading
load("analysis.rda")
ls()  # See loaded objects

Alternative: saveRDS and readRDS

# Save single object (more flexible)
saveRDS(data, file = "data.rds")

# Load with custom name
loaded_data <- readRDS("data.rds")

# This is cleaner than save/load for single objects

Text File Operations

Write to Text Files

# Write simple text
cat("This is a line of text\n", file = "output.txt")

# Append to file
cat("Another line\n", file = "output.txt", append = TRUE)

# Write vectors
x <- c(1, 2, 3, 4, 5)
cat("Values:", x, "\n", file = "output.txt")

# Write formatted output
writeLines(c("Line 1", "Line 2", "Line 3"), con = "output.txt")

Using sink() for Redirecting Output

# Start capturing output to file
sink("analysis.txt")

cat("Analysis Results\n")
cat("================\n")
print(summary(mtcars))
cat("\nCorrelation Matrix:\n")
print(cor(mtcars[, 1:5]))

# Stop capturing (return to console)
sink()

# Append to file
sink("analysis.txt", append = TRUE)
cat("\nAdditional Analysis\n")
sink()

Working with Paths

# Get current working directory
wd <- getwd()
print(wd)

# File path manipulation
file.path("data", "raw", "2023", "january.csv")
# Returns: "data/raw/2023/january.csv"

# Get file directory
dirname("data/file.csv")  # "data"

# Get file name
basename("data/file.csv")  # "file.csv"

# Path expansion
path_with_tilde <- "~/Documents/data.csv"
path.expand(path_with_tilde)  # Expands ~ to home directory

System Commands

Execute System Commands

# List files using system command
system("ls")              # On Mac/Linux
system("dir")             # On Windows

# Capture output
output <- system("ls", intern = TRUE)
print(output)

# Check R version
R.Version()

# Session information
sessionInfo()

Practical Workflow Example

# Complete file operations workflow
print("=== File Operations Workflow ===")

# 1. Create project directory structure
if (!dir.exists("project")) {
  dir.create("project", recursive = TRUE)
  dir.create("project/data/raw", recursive = TRUE)
  dir.create("project/data/processed", recursive = TRUE)
  dir.create("project/output", recursive = TRUE)
  dir.create("project/scripts", recursive = TRUE)
}

# 2. Create sample data
sample_data <- data.frame(
  id = 1:100,
  value = rnorm(100),
  group = rep(c("A", "B"), 50)
)

# 3. Save raw data
save(sample_data, file = "project/data/raw/sample_data.rda")

# 4. Save processed version
processed <- sample_data[sample_data$value > 0, ]
save(processed, file = "project/data/processed/filtered_data.rda")

# 5. Export to CSV
write.csv(processed, file = "project/data/processed/filtered_data.csv")

# 6. Create analysis report
sink("project/output/report.txt")
cat("Data Analysis Report\n")
cat("====================\n")
cat("Total observations:", nrow(sample_data), "\n")
cat("Filtered observations:", nrow(processed), "\n")
cat("Mean value:", mean(sample_data$value), "\n")
sink()

# 7. List generated files
cat("Generated files:\n")
print(list.files("project", recursive = TRUE))

Best Practices

  1. Always check existence - Use file.exists() before reading
  2. Use absolute paths - More reliable than relative paths
  3. Create directories safely - Use recursive = TRUE when needed
  4. Clean up temp files - Remove unnecessary files to save space
  5. Organize projects - Use clear directory structure
  6. Back up important data - Keep copies of critical files
  7. Use native RDA format - Efficient for R objects
  8. Handle errors gracefully - Check operation success

Common Questions

Q: What’s the difference between RDA and RDS formats? A: RDA can save multiple objects, RDS saves single objects. RDS is more flexible for single object workflows.

Q: How do I check if a path is absolute or relative? A: Use startsWith(path, "/") for Unix or check for drive letter on Windows

Q: Can I delete a directory with files? A: Yes, use unlink(dir, recursive = TRUE)

Q: How do I get the size of all files in a directory? A: Use sum(file.info(list.files(full.names=T))$size)

Download R Script

Get all code examples from this tutorial: file-system-operations-examples.R