在 data.frame 与 zoo 之间转换
在处理真实世界的时间序列数据时,您常常会从电子表格和表格型数据中导入数据——这类数据的格式类似于数据框。将数据转换为 zoo 对象后,您就能更好地开展时间序列分析!
同样地,把时间序列转换回数据框,能让您以一种在 R 之外的软件和编程语言中也广泛可读的格式来操作和导出数据。
card_prices 时间序列(包含 3 张交易卡的日均价格),以及 lubridate、zoo 和 ggplot2 包已为您加载。
本练习是课程的一部分
R 中的时间序列数据处理
练习说明
将
card_prices的zoo对象"加固"(fortify)为数据框,并赋值给cards_df。将
cards_df的 3 列价格相加,并赋值给新列cards_df$total_price。使用
cards_df的Index和total_price列创建一个新的时间序列total_price_zoo。对
total_price_zoo使用 autoplot 可视化。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Fortify to data frame: cards_df
___ <- ___
# Add together the three price columns from cards_df
___ <- ___ + ___ + ___
# Create the total_price_zoo time series
___ <- ___
# Generate an autoplot of the new time series
___