1. Saving and exporting xts objects
Another basic task in time series manipulation is to save your data. We'll cover two options for saving time series data in R.
2. Saving as rds
First, the most efficient and reliable way to store your xts objects is to save them as rds files using the saveRDS() command.
When you load time series data back into R from an rds file using readRDS(), the data will keep all of its characteristics - including the time-index of the xts object.
For this reason, this is the preferred method for saving xts objects when working in R.
3. Saving as csv
But what if you need to share your time series data with a colleague who doesn't use R?
In this scenario, you shouldn't save your data to an rds file because your colleague may not be able to read the file.
Instead, you can export your xts object to an easily readable file type, such as a comma-separated values (or CSV) file. To do this, you should use the write.zoo() command, which allows you to specify the format of your export file.
However, with compatibility comes a loss of information. When you reopen the CSV file using read.zoo(), you'll find that your data is encoded as a zoo object rather than an xts object. Before proceeding with your analysis, you'll need to re-convert the object to xts.
4. Let's practice!
OK, let's practice!