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

Writing Files

In this page:

  1. Writing Files

Writing Files

write.csv(df, "file.csv") writes a data frame out to a CSV file, and row.names = FALSE is commonly added to avoid writing an extra unnamed index column. This is the standard way to export processed data from R for use elsewhere. writeLines() is a lower-level alternative for writing plain text.

Note: Always pass row.names = FALSE to write.csv() unless you specifically want R's row index written as an extra column.

Example: Writing Files

markup
df <- data.frame(name = c("Alice", "Bob"), age = c(30, 25))

write.csv(df, "output.csv", row.names = FALSE)

contents <- readLines("output.csv")
print(contents)
🔒

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.