开始使用免费开始使用

使用 merge() 并绘制随时间变化的图

现在,您已经有了与航班数据相同时间段(2010-2015)且相同频率(按月)的气温数据,可以进行合并了。

要按列合并 xts 对象,可使用 merge()。当两个 xts 对象具有相同的周期性时,merge() 通常可以将信息合并到合适的行中。即使 xts 对象不共享相同的周期性,merge() 也会在不同周期之间保持对象的正确时间顺序。

在本练习中,您将按列合并两个 xts 对象,并生成新图,探究航班延误与气温的关系。temps_monthlyflights_xts 已在您的工作区中可用。

本练习是课程的一部分

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

查看课程

练习说明

  • 使用 merge() 合并 flights_xtstemps_monthly。由于这两个 xts 对象共享周期性,您的合并命令应将气温数据放入 flights_xts 对象中对应的行。请注意,您在合并时列出对象的顺序将决定合并后对象中列的位置。为保持一致性,请先放入 flights_xts,再放入 temps_monthly
  • 查看合并后的 xts 对象(flights_temps)的前几行,确认合并是否成功。您应当看到气温数据与航班数据对齐。
  • 使用 plot.zoo() 生成一个包含 flights_tempspct_delaytemps_monthly 两列的单一图形。请确保对子集化相关列,并将 plot.type 指定为 "single"lty 参数保持不变。

交互式实操练习

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

# Use merge to combine your flights and temperature objects
flights_temps <- merge(___, ___)

# Examine the first few rows of your combined xts object


# Use plot.zoo to plot these two columns in a single panel
plot.zoo(___[,c("___", "___")], plot.type = "___", lty = lty)
legend("topright", lty = lty, legend = labels, bg = "white")
编辑并运行代码