How to Use write.table in R?
In this article, we will learn how to use the write.table() in the R Programming Language. The write.table() function is used to export a dataframe or matrix to a file in the R Language. This function converts a dataframe into a text file in the R Language and can be used to write dataframe into a variety of space-separated files for example CSV( comma separated values) files.
Syntax:
write.table( df, file)
where:
- df: determines the dataframe to be converted.
- file: determines the file name to write data into with full path.
Example:
Here, we will write a dataframe into a space-separated text file in the R Language by using write.table().
R
sample_data <- data.frame ( name= c ( "Geeks1" , "Geeks2" , "Geeks3" ,
"Geeks4" , "Geeks5" , "Geeks6" ),
value= c ( 11, 15, 10, 23, 32, 53 ) )
write.table ( sample_data, file= 'sample.txt' )
|
Output:
Write dataframe into manual symbol separated text file:
To write a dataframe into a text file separated by a manual symbol, we use the sep parameter to determine the symbol that separates the data in the text file. In this way, we can write comma-separated-values, tab-separated values, etc.
Syntax:
write.table( df, file, sep)
where:
- df: determines the dataframe to be converted.
- file: determines the file name to write data into with full path.
- sep: determines with a symbol for separation. Default is a space.
Example:
Here, we will write a dataframe into a comma-separated text file in the R Language by using write.table() function with sep parameter.
R
sample_data <- data.frame ( name= c ( "Geeks1" , "Geeks2" , "Geeks3" ,
"Geeks4" , "Geeks5" , "Geeks6" ),
value= c ( 11, 15, 10, 23, 32, 53 ) )
write.table ( sample_data, file= 'sample.txt' , sep= "," )
|
Output:
Last Updated :
19 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...