Session Ready
Exercise

Exporting xts objects

Now that you can read raw data into xts and zoo objects, it is only natural that you learn how to reverse the process.

There are two main use cases for exporting xts objects. First, you may require an object to persist across sessions for use in later analysis. In this case, it is almost always best to use saveRDS() and readRDS() to serialize single R objects.

Alternatively, you may find yourself needing to share the results of your analysis with others, often expecting the data to be consumed by processes unaware of both R and xts. Most of us would prefer not to think of this horrible fate for our data, but the real world mandates that we at least understand how this works.

One of the best ways to write an xts object from R is to use the zoo function write.zoo(). In this exercise you'll take your temporary data and write it to disk using write.zoo().

Instructions
100 XP
  • Convert sunspots to xts and save it as sunspots_xts.
  • The temporary file name will be loaded for you.
  • Using write.zoo(), save the sunspots_xts data to the tmp file.
  • Read the tmp file back into R using read.zoo(). Call this sun.
  • Convert sun to xts using the as.xts() function. Call this sun_xts.