You can use write.table() function to export data frame to a text file in R.
The following method shows how you can do it with syntax.
Method: Use write.table() Function
write.table(dataframe, path = "C:\\Users\\admin\\data.txt")
The following example shows how to export data frame to text file in R.
Using write.table() Function
To use write.table() function you need to follow below steps:
Step 1: Create a Data Frame
Let first create data frame in R:
# Create data frame
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(110,120,108,95,98,105),Reading= c(110,97,95,89,80,85))
# Print data frame
print(df)
Output:
Start_date Machine_name Value Reading
1 2000-05-21 Machine1 110 110
2 1998-03-28 Machine2 120 97
3 2001-01-19 Machine3 108 95
4 2003-04-27 Machine4 95 89
5 2004-11-26 Machine5 98 80
6 2008-11-25 Machine6 105 85
The output show data frame which having five column in it.
Step 2: Use write.table() to Export Data Frame
Let see how to store this data frame in text file:
# Create data frame
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(110,120,108,95,98,105),Reading= c(110,97,95,89,80,85))
# Export data frame to text file
write.table(df,"machine.txt")
Here using above R script we can export data frame to text file.