2017-06-20 53 views
0

假設我有一個變量的面板數據直到2016年,並且我預計2017年的變量的總增長率(1 +增長率)和2018.如何使用預計的總增長率將利息變量擴展到2018年?使用另一列的預測增長率擴展面板數據列

這裏是我擁有的數據的一個例子:

Country Year var g 
A 2016 5 1.01 
B 2016 6 0.98 
C 2016 7 1.05 
A 2017 NA 1.06 
B 2017 NA 0.97 
C 2017 NA 1.09 
A 2018 NA 1.04 
B 2018 NA 1.02 
C 2018 NA 0.91 

我想用g項目var更換來港在2017年和2018年。因此,在2017年,對於CountryA,var將是5*1.06=5.3

產生的數據幀應該是這樣的:

Country Year var g 
A 2016 5 1.01 
B 2016 6 0.98 
C 2016 7 1.05 
A 2017 5.3 1.06 
B 2017 5.82 0.97 
C 2017 7.63 1.09 
A 2018 5.512 1.04 
B 2018 5.9364 1.02 
C 2018 6.9433 0.91 

回答

0

試試這個:

df <- data.frame(Country = rep(c("A","B","C"), 3), Year = rep(2016:2018, each = 3), var = c(5,6,7,NA,NA,NA,NA,NA,NA), g = c(1.01,0.98,1.05,1.06,0.97,1.09,1.04,1.02,0.91)) 

x <- split(df, df$Country) 
for(l in 1:length(x)){ # list length 
    for(i in 1:nrow(x[[l]])){ # matrix rows 
    # because I'm using i + 1, we want to stop if we are on the final i 
    if(i != nrow(x[[l]])) x[[l]]$var[i+1] <- x[[l]]$var[i] * x[[l]]$g[i+1] 
    } 
} 
df2 <- do.call(rbind,x) # recombine the list by row 
df2 <- df2[order(df2[,"Year"]),] # order so that it is sorted by year again 
rownames(df2) <- NULL # we can remove the names with 

df2 
#Country Year var g 
#1  A 2016 5.0000 1.01 
#2  B 2016 6.0000 0.98 
#3  C 2016 7.0000 1.05 
#4  A 2017 5.3000 1.06 
#5  B 2017 5.8200 0.97 
#6  C 2017 7.6300 1.09 
#7  A 2018 5.5120 1.04 
#8  B 2018 5.9364 1.02 
#9  C 2018 6.9433 0.91 
+0

非常感謝。它工作得很好。 – user49017

+0

沒問題歡呼聲 –