CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Case Study: Analyzing City Time Series Data in R

Afficher le cours

Instructions

  • Use two calls to class() to check that the date columns in temps_1 and temps_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 and temps_2) into a separate xts object. Be sure to specify the relevant time-based column for the order.by argument. Also remember to remove the time-based column using the data[, -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() and last() to identify data from the first 3 days of the last month of the first year in temps_xts.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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"), "___")
Modifier et exécuter le code