Merging using rbind()
Now that you know the structure and scope of your temperature data, your next task will be to convert these objects to xts
and merge them using rbind()
.
Before you can convert an object to xts, you need to identify the column that will form the time index and ensure it is encoded as a time-based object. In this case, you'll want to check the class of the date
column in temps_1
and temps_2
. Once you identify the appropriate time-based index, you can encode both objects to xts and merge by row.
The temps_1
and temps_2
objects are available in your workspace and the xts
package has been loaded for you.
This exercise is part of the course
Case Study: Analyzing City Time Series Data in R
Exercise instructions
- Use two calls to
class()
to check that thedate
columns intemps_1
andtemps_2
are encoded as time-based objects (Date, POSIXct, POSIXlt, yearmon, etc.). - Use
as.xts()
to encode each of your temperature data frames (temps_1
andtemps_2
) into a separate xts object. Be sure to specify the relevant time-based column for theorder.by
argument. Also remember to remove the time-based column using thedata[, -column]
format. - Use two calls to
head()
to confirm that your new xts objects are properly formatted. - Use
rbind()
on your xts objects to merge them into a single object:temps_xts
. - Use a combination of
first()
andlast()
to identify data from the first 3 days of the last month of the first year intemps_xts
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Confirm that the date column in each object is a time-based class
class(___)
class(___)
# Encode your two temperature data frames as xts objects
temps_1_xts <- as.xts(___[, -4], order.by = ___)
temps_2_xts <- ___
# View the first few lines of each new xts object to confirm they are properly formatted
head(___)
head(___)
# Use rbind to merge your new xts objects
temps_xts <- ___
# View data for the first 3 days of the last month of the first year in temps_xts
___(___(first(___, "1 year"), "1 month"), "___")