2017-10-10 129 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代表的利益

變量