R Data Exporting
Data Exporting
- R can also write data to external files
- R can write in various file formats like csv, xlsx, xml etc…
- Steps to store the data externally are as follows:
- Step 1: Set the path of the location to the file’s location
- getwd() # gives the current directory
- setwd(“path”) # sets the path, e.g. setwd(“D:/R/dataset/”)
- Step 2: Select the object (vector, matrix, dataframe etc…) that should be stored, e.g. sampledata
- Step 3: Use ‘write’ command to store the data
- write.csv () # writes to .csv file
- write.xlsx () # writes to .xlsx file
- write.table() # writes to .txt files
Syntax
write.xlsx(sampledata, file = "sample.xlsx", sheetName = "Sample")
If we need to save the object ‘tk’ to “d:/R/” location as a file with name “Truck”
Step 1: Set the path
Step 2: Ensure xlsx package is installed
Step 3: Use write.xlsx () from xlsx package
CODE/PROGRAM/EXAMPLE
write.xlsx(tk, "Truck.xlsx", sheetName = "Details")