Here is the general syntax that you may use to export your DataFrame to a text file in R:
write.table(DataFrame Name, "Path to export the DataFrame\\File Name.txt")
If you wish to add a separator, such as a comma separator, then use:
write.table(DataFrame Name, "Path to export the DataFrame\\File Name.txt", sep=",")
You may exclude the row numbers as follows:
write.table(DataFrame Name, "Path to export the DataFrame\\File Name.txt", row.names=FALSE)
Optionally, you may omit the quotes:
write.table(DataFrame Name, "Path to export the DataFrame\\File Name.txt", quote=FALSE)
Steps to Export a DataFrame to a Text File in R
Step 1: Create a DataFrame
To start, let’s create a simple DataFrame with the following information:
df <- data.frame(product = c("computer", "monitor", "keyboard", "printer", "tablet"), price = c(800, 450, 100, 150, 300) ) print(df)
You’ll then get the following DataFrame:
product price
1 computer 800
2 monitor 450
3 keyboard 100
4 printer 150
5 tablet 300
The ultimate goal is to export the above DataFrame to a Text file.
Step 2: Specify the path to export the file
Next, specify the path to export the file.
For example, assume that the path would be as follows:
Pay attention to several highlighted portions in the path name:
- The blue portion represents the file name to be created. For our example, the file name is “products” but you may specify another file name based on your needs
- The green portion reflects the file type of .txt
Also notice that a double backslash (‘\\’) was used within the path to avoid errors in the path name.
Before running the code in R, make sure to adjust the path to reflect the location where the text file would be stored on your computer.
Step 3: Use write.table to export the DataFrame to a text file in R
Finally, you may apply the syntax below (adjusted to your path) to export the DataFrame to a text file in R:
df <- data.frame(product = c("computer", "monitor", "keyboard", "printer", "tablet"), price = c(800, 450, 100, 150, 300) ) write.table(df, "C:\\Users\\Ron\\Desktop\\products.txt")
Here are the results:
“1” “computer” 800
“2” “monitor” 450
“3” “keyboard” 100
“4” “printer” 150
“5” “tablet” 300
If you want to add a comma separator, then use sep=”,” as follows:
df <- data.frame(product = c("computer", "monitor", "keyboard", "printer", "tablet"), price = c(800, 450, 100, 150, 300) ) write.table(df, "C:\\Users\\Ron\\Desktop\\products.txt", sep=",")
As you can see, a comma separator was added:
“1”,”computer”,800
“2”,”monitor”,450
“3”,”keyboard”,100
“4”,”printer”,150
“5”,”tablet”,300
Optionally, you may exclude the row numbers by setting row.names=FALSE:
df <- data.frame(product = c("computer", "monitor", "keyboard", "printer", "tablet"), price = c(800, 450, 100, 150, 300) ) write.table(df, "C:\\Users\\Ron\\Desktop\\products.txt", row.names=FALSE)
The row numbers would no longer appear:
“computer” 800
“monitor” 450
“keyboard” 100
“printer” 150
“tablet” 300
Alternatively, you may export the DataFrame without the quotes using quote=FALSE:
df <- data.frame(product = c("computer", "monitor", "keyboard", "printer", "tablet"), price = c(800, 450, 100, 150, 300) ) write.table(df, "C:\\Users\\Ron\\Desktop\\products.txt", quote=FALSE)
Here are the results without the quotes:
1 computer 800
2 monitor 450
3 keyboard 100
4 printer 150
5 tablet 300
You may combine multiple conditions (e.g., use comma separator, exclude row numbers and quotes) when exporting your file:
df <- data.frame(product = c("computer", "monitor", "keyboard", "printer", "tablet"), price = c(800, 450, 100, 150, 300) ) write.table(df, "C:\\Users\\Ron\\Desktop\\products.txt", sep=",", row.names=FALSE, quote=FALSE)
computer,800
monitor,450
keyboard,100
printer,150
tablet,300