在 R 中计算 monthly/quarterly/year 增量

Calculate monthly/quarterly/year delta in R

下面是数据框的样子,我如何在右侧创建列来总结 R 中的月环比差异、季度环比差异和年环比差异?谢谢

试试这个:

# test data
DF <- data.frame(Date = c(paste0(month.abb, 2015), paste0(month.abb, 2016)), 
                 Aggressive = seq(24)^2, Moderate = seq(24)^2)

Diff <- function(x, k) x - c(rep(NA, k), head(x, -k))
transform(DF, Aggressive_Mon = Diff(Aggressive, 1),
                Moderate_Mon = Diff(Moderate, 1),
                Aggressive_Qtr = Diff(Aggressive, 3),
                Moderate_Qtr = Diff(Moderate, 3),
                Aggressive_Yr = Diff(Aggressive, 12),
                Moderate_Yr = Diff(Moderate, 12))

下次请不要使用图像来显示数据,而是显示 dput(DF) 的输出。