2017-10-10 127 views

回答

0

dplyr帮助它的lag()函数,这不是一个ts(时间序列)数据帧。下面一个例子:

#using the library "dplyr" 
library(dplyr) 
#setting seed 
set.seed(20) 
#creating a random dataframe 
df <- data.frame(date=paste(rep(2000:2017, each=4),"Q",rep(1:4, 18)), GDP= cumsum(sample(c(-0.5, 3), 72, TRUE))) 
#calculating the annual percentage change 
df <- df %>% mutate(change=(GDP-lag(GDP,4))/lag(GDP,4)*100) 

在月度数据的情况下,只需设置:lag(VAR,12)其中VAR代表的利益

变量