我有一个data.frame,其中包含三列Year,Coefficent_change和Cummulative_Coefficient_changes。
Year Coefficent_change Cummulative_Coefficient_changes
--------------------------------------------------------------
2014 1,0
2015 1,1
2016 1,0
2017 1,0
2018 1,0
2019 1,0
--------------------------------------------------------------
仅针对列Cummulative_Coefficient_changes计算。因此,公式必须从buttom开始并向上。
为最后一次观察,例如,默认情况下,2019年的Cummulative_Coefficient_changes必须为1。
对于每个后续观察公式,必须具有相同的形式,
例如2018 Cummulative_Coefficient_changes2018 = Coefficent_change2019 * Cummulative_Coefficient_changes2019
例如2014年 Cummulative_Coefficient_changes2014 = Coefficent_change2015 * Cummulative_Coefficient_changes2015
因此,表的结尾应类似于下表。
Year Coefficent_change Cummulative_Coefficient_changes
--------------------------------------------------------------
2014 1,0 1,1
2015 1,1 1,0
2016 1,0 1,0
2017 1,0 1,0
2018 1,0 1,0
2019 1,0 1,0
--------------------------------------------------------------
那么有人可以帮助我如何使用dplyr解决此问题吗?
答案 0 :(得分:1)
这里是一种方法:
library(dplyr)
# read sample data
my_data <- read.table(text = " Year Coefficient_change
2014 1.0
2015 1.1
2016 1.0
2017 1.0
2018 1.0
2019 1.0", header = T)
my_data
# add the wanted column filled with the defualt value
my_data$Cummulative_Coefficient_changes <- 1.0
# now calculate the right values
my_data %>%
arrange(desc(Year)) %>% # arrange data [descending] by Year
mutate(Cummulative_Coefficient_changes =
lag(Coefficient_change, default = 1.0)*
lag(Cummulative_Coefficient_changes, default = 1.0))