In this tutorial, you will learn about how to export (write) data to any new file format such as txt, CSV, etc. The data scientist usually stores data in Excel Spreadsheets. In the R programming language, there are several R packages such as XLConnect, xlsx, RExcel, gdata, RODBC, etc to access the data from Excel Spreadsheets. The R users or programmers save the spreadsheets (data) mostly in CSV format/files to take advantage of R‘s built-in functionalities to manipulate the data. So through this tutorial, you will learn the read (import) and write (export) operations from .csv files in R language.
In the below-given screenshot, you can view that only the folder R programs contain only a single CSV file name 3-1homprices. The data you created or manipulated can be stored using the write.csv()
method.
Consider the below given piece of code
write.csv(df1, file ="C:\\Users\\Desktop\\R\\R Pgms\\3-1homeprices1.csv")
The write.csv() function with the name of the data frame (df1) to be saved is given as the first argument followed by the path or location where the data needs to be stored.
After executing the above code you can view a new file 3-1homeprices1 with a .csv extension created inside the same folder.(The user can store the file based on his wish)
The screenshot below shows the exported file.
Earlier we imported a CSV file named 3-1homeprices using read.csv()
function. Now let us export this 3-1homeprices as a .txt file with tab-separated.The write.table()
function is used
write.table(df1,file ="C:\\Users\\Desktop\\R\\R Pgms\\3-1homeprices1.txt",sep = "\t",row.names=FALSE)
With arguments such as the name of the data frame (df1), the path where you need to store the data frame with a new file name with .txt as an extension, then specify the separator to separate the data with a space or tab.
The code after execution you can view the newly created txt file at the specified name. In our example, a new file named 3-1homeprices1 gets created and stored in a folder for later use as shown below screenshot.
Once after opening the .txt file you can view the below data