开始使用免费开始使用

在 data.frame 与 zoo 之间转换

在处理真实世界的时间序列数据时,您常常会从电子表格和表格型数据中导入数据——这类数据的格式类似于数据框。将数据转换为 zoo 对象后,您就能更好地开展时间序列分析!

同样地,把时间序列转换回数据框,能让您以一种在 R 之外的软件和编程语言中也广泛可读的格式来操作和导出数据。

card_prices 时间序列(包含 3 张交易卡的日均价格),以及 lubridatezooggplot2 包已为您加载。

本练习是课程的一部分

R 中的时间序列数据处理

查看课程

练习说明

  • card_priceszoo 对象"加固"(fortify)为数据框,并赋值给 cards_df

  • cards_df 的 3 列价格相加,并赋值给新列 cards_df$total_price

  • 使用 cards_dfIndextotal_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
___
编辑并运行代码