Saving time - II
You've saved your flights_xts
data to a rds file for future use. But what if you'd like to share your data with colleagues who don't use R?
A second option for saving xts objects is to convert them to shareable formats beyond the R environment, including comma-separated values (CSV) files. To do this, you'll use the write.zoo() command.
Once you've successfully exported your xts object to a csv file, you can load the data back into R using the read.zoo()
command. Unlike readRDS
, however, you will need to re-encode your data to an xts object (using as.xts
).
This exercise is part of the course
Case Study: Analyzing City Time Series Data in R
Exercise instructions
- Use
write.zoo()
to save theflights_xts
data to"flights_xts.csv"
. Be sure to specify comma-separated values (","
) using thesep
argument. - Read your file back into R using
read.zoo()
. Specify the name of the file you exported as well as the method used to separate the data. Take a look at the other arguments but don't change the code. Save this new object asflights2
. - Encode your
flights2
object back into xts usingas.xts()
. Save your new xts object asflights_xts2
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Export your xts object to a csv file using write.zoo
write.zoo(flights_xts, file = "___", sep = "___")
# Open your saved object using read.zoo
flights2 <- read.zoo("___", sep = "___", FUN = as.Date, header = TRUE, index.column = 1)
# Encode your new object back into xts
flights_xts2 <- as.xts(___)
# Examine the first five rows of your new flights_xts2 object