How to Export a DataFrame to a Text File in R

To export a DataFrame to a text file in R:

write.table(df, "Path to export the DataFrame\\File Name.txt")

Add a separator, such as a comma separator:

write.table(df, "Path to export the DataFrame\\File Name.txt", sep=",")

Exclude the row numbers:

write.table(df, "Path to export the DataFrame\\File Name.txt", row.names=FALSE)

Omit the quotes:

write.table(df, "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, 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)

The result is 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:

C:\\Users\\Ron\\Desktop\\products.txt

Pay attention to several highlighted parts in the path name:

  • The blue part represents the file name to be created. For our example, the file name is “products
  • The green part 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, 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:

“product” “price”
“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:

“product”,”price”
“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:

“product” “price”
“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:

product price
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 exclude 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)

The result:

product,price
computer,800
monitor,450
keyboard,100
printer,150
tablet,300

Leave a Comment