There are multiple function are available in R to export data.
The following methods show how you can do it with syntax.
Method 1: Exporting as a Text File
write.table(data,"file.txt",sep="\t")
Method 2: Exporting as a Excel File
library("writexl")
write_xlsx(df,"file.xlsx")
Method 3: Exporting as a CSV File
write.csv(data,"file.csv")
Method 4: Exporting as a RDA File
save(data,file= "file.Rda")
The following examples show how to export data in R, depending on file format.
Using write.table() Function
You can use write.table() function to exporting data to text file:
# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
Value = c(108,99,135,95,98,105),Reading= c(110,97,91,89,80,85))
# Export data to text file
write.table(df,"Machine.txt",sep="\t")
Here in the above code we export dataframe df to Machine.txt using write.table() function in R.
Using write_xlsx() Function
To export data to excel file you can use write_excel() function writexl package.
# Import library
library("writexl")
# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
Value = c(108,99,135,95,98,105),Reading= c(110,97,91,89,80,85))
# Export data to excel file
write_xlsx(df,"Machine.xlsx")
Here in the above code we export dataframe to Machine.xlsx file using write_xlsx function in R.
Using write.csv() Function
Suppose you want to export data to CSV file then use write.csv() function:
# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
Value = c(108,99,135,95,98,105),Reading= c(110,97,91,89,80,85))
# Export data to CSV file
write.csv(df,"Machine.csv")
In the above code we export dataframe to CSV file using write.csv() function in R.
Using save() Function
Using save() function you can export data to RDA file in R:
# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
Value = c(108,99,135,95,98,105),Reading= c(110,97,91,89,80,85))
# Export data to RDA file
save(df,file = "Machine.Rda")
In the above code we export dataframe to RDA file using save() function.