Open In App

Exporting Data from scripts in R Programming

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In R, when a program terminates, all data is lost unless it is exported to a file. Exporting data ensures its preservation, even after the program ends, and allows for easy sharing, storage, and transfer between systems.

Exporting data is essential to prevent loss of information. It allows for:

  1. Data Preservation: Storing data in files retains it beyond program termination.
  2. Efficiency: Exporting data saves time by eliminating the need for manual entry each time the program runs.
  3. Portability: Data can be transferred across different systems without modification.

R supports various file formats for exporting data, including .txt (tab-separated), .csv (comma-separated), or cloud storage.Exporting data to a text file.

Exporting data to a text file

One of the important formats to store a file is in a text file. R provides various methods that one can export data to a text file.

1. write.table():

The R base function write.table() can be used to export a data frame or a matrix to a text file.

Syntax:

write.table(x, file, append = FALSE, sep = ” “, dec = “.”, row.names = TRUE, col.names = TRUE) 

Parameters: 

  • x: a matrix or a data frame to be written. 
  • file: a character specifying the name of the result file.
  • sep: the field separator string, e.g., sep = “\t” (for tab-separated value). 
  • dec: the string to be used as decimal separator. Default is “.” 
  • row.names: either a logical value indicating whether the row names of x are to be written along with x, or a character vector of row names to be written. 
  • col.names: either a logical value indicating whether the column names of x are to be written along with x, or a character vector of column names to be written.

Example:

We are creating a data frame df with three columns: Name, Language, and Age. Then, we use the write.table() function to export the data frame to a tab-separated text file myDataFrame.txt, including row names but excluding column names.

R
df = data.frame( 
"Name" = c("Amiya", "Raj", "Asish"), 
"Language" = c("R", "Python", "Java"), 
"Age" = c(22, 25, 45) 
) 

write.table(df,
            file = "myDataFrame.txt",
            sep = "\t",
            row.names = TRUE,
            col.names = NA)

Output:

textfile1

2. write_tsv():

This write_tsv() method is also used for to export data to a tab separated (“\t”) values by using the help of readr package.

Syntax:

write_tsv(file, path) 

Parameters: 

  • file: a data frame to be written 
  • path: the path to the result file

Example:

We are installing the readr package and loading it using library(). Next, we create a data frame df with three columns: Name, Language, and Age. Then, we use the write_tsv() function from the readr package to export the data frame to a tab-separated text file named MyDataFrame.txt.

R
install.packages("readr")
library(readr)

df = data.frame( 
"Name" = c("Amiya", "Raj", "Asish"), 
"Language" = c("R", "Python", "Java"), 
"Age" = c(22, 25, 45) 
) 

write_tsv(df, path = "MyDataFrame.txt")

Output:

textfile2

Exporting data to a csv file

Another popular format to store a file is in a csv(comma-separated value) format. R provides various methods that one can export data to a csv file.

1. write.table():

The R base function write.table() can also be used to export a data frame or a matrix to a csv file.

Syntax:

write.table(x, file, append = FALSE, sep = ” “, dec = “.”, row.names = TRUE, col.names = TRUE) 

Parameters: 

  • x: a matrix or a data frame to be written.
  • file: a character specifying the name of the result file. 
  • sep: the field separator string, e.g., sep = “\t” (for tab-separated value). 
  • dec: the string to be used as decimal separator. Default is “.” 
  • row.names: either a logical value indicating whether the row names of x are to be written along with x, or a character vector of row names to be written. 
  • col.names: either a logical value indicating whether the column names of x are to be written along with x, or a character vector of column names to be written.

Example:

We are creating a data frame df with three columns: Name, Language, and Age. Then, we use the write.table() function to export the data frame to a CSV file named myDataFrame.csv, with tab-separated values (sep = "\t") and without row names (row.names = FALSE).

R
df = data.frame( 
"Name" = c("Amiya", "Raj", "Asish"), 
"Language" = c("R", "Python", "Java"), 
"Age" = c(22, 25, 45) 
) 

write.table(df,
            file = "myDataFrame.csv",
            sep = "\t",
            row.names = FALSE,
            )

Output:

2. write.csv():

This write.csv() method is recommendable for exporting data to a csv file. It uses “.” for the decimal point and a comma (“, ”) for the separator. 

Syntax:

write.csv(x, file, row.names = TRUE, …)

Parameters:

  • x: The data frame or matrix to be written to the file.
  • file: The name of the output file (with .csv extension).
  • row.names: A logical value indicating whether to include row names (default is TRUE).

Example:

We are creating a data frame df with three columns: Name, Language, and Age. Then, we use the write.csv() function to export the data frame to a CSV file named my_data.csv. The write.csv() function automatically separates values with commas and includes column names by default.

R
df = data.frame( 
"Name" = c("Amiya", "Raj", "Asish"), 
"Language" = c("R", "Python", "Java"), 
"Age" = c(22, 25, 45) 
) 

write.csv(df, file = "my_data.csv")

Output:

3. write.csv2():

This method is much similar as write.csv() but it uses a comma (“, ”) for the decimal point and a semicolon (“;”) for the separator.

Syntax:

write.csv2(x, file, row.names = TRUE, …)

Parameters:

  • x: The data frame or matrix to be written to the file.
  • file: The name of the output file (with .csv extension).
  • row.names: A logical value indicating whether to include row names (default is TRUE).

Example:

We are creating a data frame df with three columns: Name, Language, and Age. Then, we use the write.csv2() function to export the data frame to a CSV file named my_data.csv.

R
df = data.frame( 
"Name" = c("Amiya", "Raj", "Asish"), 
"Language" = c("R", "Python", "Java"), 
"Age" = c(22, 25, 45) 
) 

write.csv2(df, file = "my_data.csv")

Output:

4. write_csv():

This method is also used for to export data to a comma separated (“, ”) values by using the help of readr package.

Syntax: 

write_csv(file, path) 

Parameters:

  • file: a data frame to be written 
  • path: the path to the result file

Example:

We are installing the readr package and loading it using library() function. Next, we create a data frame df with three columns: Name, Language, and Age. After that, we use the write_csv() function from the readr package to export the data frame to a CSV file named MyDataFrame.csv. This function automatically separates the values with commas and saves the data in a CSV format.

R
install.packages("readr")
library(readr)

df = data.frame( 
  "Name" = c("Amiya", "Raj", "Asish"), 
  "Language" = c("R", "Python", "Java"), 
  "Age" = c(22, 25, 45) 
) 

write_csv(df, path = "MyDataFrame.csv")

Output:

In this article, we explored how to export data from R scripts using functions like write.table(), write_tsv(), and write.csv(), enabling efficient data storage and transfer in various formats.



Next Article
Article Tags :

Similar Reads