The write.table() function is used to export data to text file in R.

The following method shows how you can do it with syntax.

Method 1: Use write.table() Function

write.table(dataframe,"file_name.txt")

You can provide separator in values of exported file but by default it seperated by single space. For separator you can use sep argument.

For example, you could choose tab as a separator:

write.table(dataframe,"file_name.txt",sep='\t')

The following example shows how to use write.table() function in R.

Export Data Frame to Text File

Follow the below steps to use this function:

Step 1: Create a Data Frame

First start with creating dataframe :

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F"),
                 Pressure=c(78.2, 71.7, 80.21, 83.12, 82.56, 79.50),
                 Temperature=c(31, 35, 36, 36, 38, 32),
                 Status=c(TRUE,TRUE,FALSE,FALSE,FALSE,TRUE))

# Print data frame
print(df)

Output:

Machine_name Pressure Temperature Status
1            A    78.20          31   TRUE
2            B    71.70          35   TRUE
3            C    80.21          36  FALSE
4            D    83.12          36  FALSE
5            E    82.56          38  FALSE
6            F    79.50          32   TRUE

The output shows dataframe that we created using data.frame() function.

Step 2: Using write.table() Exporting Data Frame

Now use write.table() function to export df to sample.txt file:

# Export data frame to text file
write.table(df, file='r\\sample.txt')

By following these steps, you can export data frames or matrices to text files in R.