Writing to Files in R Programming

R programming 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.

Writing Data to CSV files

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:

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:

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 as:

library("xlsx")

General syntax of using it is:

write.xlsx(my_data, file = “result.xlsx”, sheetName = “my_data”, append = FALSE).

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.