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:
write.csv(my_data, file = "my_data.csv") write.csv2(my_data, file = "my_data.csv")
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:
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:
and can be loaded as:
General syntax of using it is:
write.xlsx(my_data, file = “result.xlsx”, sheetName = “my_data”, append = FALSE).