首页 > 解决方案 > 如何使用另一列计算 r 中的时间序列列?

问题描述

我是 r 的新手,我正在尝试计算一些基本的东西。我有 2 个时间序列列,时间和速度,在数据框中我正在尝试添加加速度列但不知道该怎么做:

df_x$Acceleration <- df_x$Velocity.....

我想使用的时间序列加速公式是:

基本上我有一个问题如何计算分子。

标签: r

解决方案


How about

t.plus.one <- 1:nrow(df_x) + 1
t.minus.one <- 1:nrow(df_x) - 1
t <- 1:nrow(df_x)

df_x$Acceleration <- (df_x$Velocity[t.plus.one] - df_x$Velocity[t.minus.one]) / t

This will of course lead to an error, because the indices are not in range. However, you can easily fix that buy constructing the df such that this does not occur.


推荐阅读