-1
什麼是簡單的方法來計算R中變量的年變化率,而不是時間序列(例如GDP,通貨膨脹等)?R的年變化率(增長率)與dplyr滯後
什麼是簡單的方法來計算R中變量的年變化率,而不是時間序列(例如GDP,通貨膨脹等)?R的年變化率(增長率)與dplyr滯後
包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代表的利益