Open In App

Writing to Files in R Programming

Improve
Improve
Like Article
Like
Save
Share
Report

R programming Language is one of the very powerful languages specially used for data analytics in various fields. Analysis of data means reading and writing data from various files like excel, CSV, text files, etc. Today we will be dealing with various ways of writing data to different types of files using R programming. 

R – Writing to Files 

Writing Data to CSV files in R Programming Language

CSV stands for Comma Separated Values. These files are used to handle a large amount of statistical data. Following is the syntax to write to a CSV file: 

Syntax: 

R




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


Here, 

csv() and csv2() are the function in R programming. 

  • write.csv() uses “.” for the decimal point and a comma (“, ”) for the separator.
  • write.csv2() uses a comma (“, ”) for the decimal point and a semicolon (“;”) for the separator.

Writing Data to text files

Text files are commonly used in almost every application in our day-to-day life as a step for the “Paperless World”. Well, writing to .txt files is very similar to that of the CSV files. Following is the syntax to write to a text file: 

Syntax: 

R




write.table(my_data, file = "my_data.txt", sep = "")


Writing Data to Excel files

To write data to excel we need to install the package known as “xlsx package”, it is basically a java based solution for reading, writing, and committing changes to excel files. It can be installed as follows: 

install.packages("xlsx")

and can be loaded and General syntax of using it is: 

R




library("xlsx")
 
write.xlsx(my_data, file = "result.xlsx",
           sheetName = "my_data", append = FALSE).


 



Last Updated : 25 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads