3.5 Writing to text file
You can also write tabular data to text files. Let’s say we are only interested in keep track of the station number and their name, so we will write this to a text file:
write.table(stations[,"Station.name"], file="stationName.tsv", row.names = T,
col.names = F, quote=F, sep="\t")
What is happening here * file
- specifies the output file path, we are using the current working directory * row.names
- specifies the row names are written to the file * col.names
- specifies no column names are written to the file * quote
- specifies do not use quotation symbols in the output * sep
- specifies the character to use for separating the columns, in our case we are using tabs
Enter the write.table()
command above and then examine the output file. You should be able to click on the filename in the “Files” tab to view the file.
Then experiment with different parameter settings, you can keep the output file open to observe how the output changes with each parameter change. For example, try
-
row.names=F
-
quote=T
-
sep=‘,’
If you are often dealing with comma-separated files then you can simple use the read.csv()
and write.csv()
functions. These functions have the parameter sep
set to comma by default.