0Pricing
R Academy · Lesson

Writing Data to Multiple Formats

Export data frames to CSV, Excel, and RDS with write_csv() and writexl.

Writing Data from R

Reading data is only half the story — you also need to save results. R supports multiple output formats: CSV for universal compatibility, RDS for preserving R objects exactly, Parquet for high-performance analytics, and more. Choose the format based on your audience and use case.

library(readr)

# Create a sample data frame to write
df <- data.frame(
  name = c('Alice','Bob','Carol'),
  score = c(85.5, 92.0, 78.3),
  grade = c('B','A','C'),
  passed = c(TRUE, TRUE, TRUE)
)

print(df)
cat('\nWe will save this in multiple formats!')

write_csv() — Save as CSV

write_csv(df, 'file.csv') saves a data frame as a comma-separated file. It does NOT write row names by default (unlike write.csv()). The result is always UTF-8 encoded. It invisibly returns the data frame, so it can be used in pipes.

library(readr)

df <- data.frame(
  name = c('Alice','Bob','Carol'),
  score = c(85, 92, 78)
)

# Write to /tmp for demonstration
write_csv(df, '/tmp/students.csv')

# Verify by reading back
read_csv('/tmp/students.csv', show_col_types=FALSE)

All lessons in this course

  1. Reading CSV Files with read_csv()
  2. Parsing TSV and Fixed-Width Files
  3. Importing Excel Files with readxl
  4. Writing Data to Multiple Formats
← Back to R Academy