Exporting Data to Text Files in R

Exporting data from R into text files is a common task for data analysts, who may need to share their results with colleagues using different software or for publication purposes. Text files, including CSV and TSV, are among the most widely used due to their simplicity and compatibility across different platforms. In this guide, you’ll learn how to take your data from R and export it into various text file formats using different functions and packages, ensuring your data can be shared and accessed by others easily.

Understanding Basic Export Functions

The base R installation provides several functions to export data into text files. The most common functions are write.table, write.csv, and write.csv2. These functions give you the power to write a data frame to a file with control over formatting details like row names, column names, separators, and text encodings.

Using write.table

The write.table function is a general-purpose exporter that can write data frames to a file with the specified separator. By changing the separator, you can generate CSV (Comma-Separated Values), TSV (Tab-Separated Values), or any other delimited text file format.


# Sample data frame
data <- data.frame(
  Column1 = 1:5,
  Column2 = letters[1:5],
  stringsAsFactors = FALSE
)

# Write the data to a tab-separated text file
write.table(data, "my_data.tsv", sep = "\t", row.names = FALSE)

If you were to look at “my_data.tsv,” you’d find the contents to be:


"Column1" "Column2"
1 "a"
2 "b"
3 "c"
4 "d"
5 "e"

Creating CSV Files with write.csv and write.csv2

For CSV file exports, R has two similar functions: write.csv and write.csv2. The difference between them lies in their default settings. write.csv uses a comma as the separator and a period for the decimal point, while write.csv2 uses a semicolon as the separator and a comma for the decimal point, which is common in some European countries.


# Write the data to a CSV file using write.csv
write.csv(data, "my_data.csv", row.names = FALSE)

The resulting file “my_data.csv” will be:


"Column1","Column2"
1,"a"
2,"b"
3,"c"
4,"d"
5,"e"

Advanced Usage with the readr Package

For a more robust and faster experience, the readr package from the tidyverse collection of packages offers additional functions for writing text files. These functions are generally faster and provide better default settings than the base R functions. The write_delim, write_csv, and write_tsv functions are the equivalent of using write.table with different separators: general, comma, and tab respectively.

write_csv and write_tsv from readr

To use readr‘s function, you first need to install and then load the package, if it’s not already installed:


# Install readr package if you haven't already
# install.packages("readr")

# Load the readr package
library(readr)

# Now export the data as CSV using write_csv
write_csv(data, "my_data_fast.csv")

# Export the data as a TSV using write_tsv
write_tsv(data, "my_data_fast.tsv")

These functions automatically set row.names = FALSE and handle factor columns and character encodings gracefully. Hence, the files “my_data_fast.csv” and “my_data_fast.tsv” will look similar to the files generated by the base R functions, without the row names:


Column1,Column2
1,a
2,b
3,c
4,d
5,e

Exporting Data with Custom Formatting

Sometimes, you need more control over the export process, for example, when you are preparing data for specific software that requires a unique file structure or when dealing with large datasets. Using write.table with additional arguments or employing the cat function to manually write files line by line gives you this control.

Customizing write.table

You can use write.table with a plethora of arguments to customize the output file. Let’s say you want to export data without quotes around character strings and with a custom file header:


# Custom header
header <- "CustomHeader1 CustomHeader2"

# Write the data with a custom header and without quotes
write.table(data, "my_custom_data.txt", sep = "\t",
            row.names = FALSE, col.names = NA, quote = FALSE)

# Add a custom header
writeLines(header, "my_custom_data.txt", sep = "\n", useBytes = TRUE)

The “my_custom_data.txt” file will start with the custom header followed by the tab-separated data:


CustomHeader1 CustomHeader2
1   a
2   b
3   c
4   d
5   e

Manually Writing Files with cat

The cat function allows you to concatenate and print objects. You can use it to manually create a text file:


# Open a connection for writing
file_conn <- file("my_manual_data.txt", "w")

# Write data line by line
cat("CustomHeader1 CustomHeader2\n", file = file_conn)
apply(data, 1, function(row) {
  cat(paste(row, collapse = "\t"), "\n", file = file_conn)
})

# Close the connection
close(file_conn)

This code gives you full control over the output file format. The “my_manual_data.txt” file will have the same contents as the custom-formatted file above.

Conclusion

In summary, R offers multiple ways to export data to text files, ranging from simple base functions like write.table and write.csv to more advanced and fast options offered by the readr package. Depending on your specific needs, whether they be quick exports or custom-formatted files, R provides the tools required to accomplish your data exportation tasks efficiently. With the knowledge of these functions and the ability to extensively customize your outputs, you can confidently share your data in the universally accessible text file formats.

About Editorial Team

Our Editorial Team is made up of tech enthusiasts who are highly skilled in Apache Spark, PySpark, and Machine Learning. They are also proficient in Python, Pandas, R, Hive, PostgreSQL, Snowflake, and Databricks. They aren't just experts; they are passionate teachers. They are dedicated to making complex data concepts easy to understand through engaging and simple tutorials with examples.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top