开始使用免费开始使用

使用 rbind() 合并

既然您已经了解了温度数据的结构和范围,下一步就是将这些对象转换为 xts,并使用 rbind() 进行合并。

在将对象转换为 xts 之前,您需要找出将作为时间索引的列,并确保其编码为基于时间的对象。在本题中,您需要检查 temps_1temps_2date 列的类。确认合适的时间索引后,您就可以将两个对象都编码为 xts,并按行合并。

temps_1temps_2 已在您的工作区中,xts 包也已为您加载。

本练习是课程的一部分

案例研究:在 R 中分析城市时间序列数据

查看课程

练习说明

  • 使用两次 class() 检查 temps_1temps_2 中的 date 列是否编码为基于时间的对象(Date、POSIXct、POSIXlt、yearmon 等)。
  • 使用 as.xts() 将两个温度数据框(temps_1temps_2)分别编码为 xts 对象。请为 order.by 参数指定相应的时间列,并记得用 data[, -column] 格式移除该时间列。
  • 使用两次 head() 确认新的 xts 对象格式正确。
  • 对这两个 xts 对象使用 rbind(),将其合并为单个对象:temps_xts
  • 结合使用 first()last(),定位 temps_xts 中"第 1 年里最后 1 个月的前 3 天"的数据。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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"), "___")
编辑并运行代码