乘以数据框中的累积乘积

时间:2018-10-02 10:28:27

标签: r dplyr

我有一个data.frame,其中有四列T,Price,Adjustment_factor和Adjusted price。因此,在“价格”列和“调整因子”列中,我想计算下表中的“调整后价格”。

T      Price     Adjustment_factor       Adjusted_price
----------------------------------------------------
2010    78,974     1,000                      79,01
2012    78,935     1,008                      78,97
2013    78,294     1,081                      78,97
2014    72,436     1,070                      78,97
2015    67,700     1,000                      78,97

计算公式每一行的Adjusted_price都不相同。

E.g For 2015  
         78,97 = 67,700 * (1,070 * 1,081 * 1,008 * 1,000)
    For 2014 
         78,97 =  72,436 * (1,081 * 1,008 * 1,000)
    For 2013 
        78,97 =  78,294 * ( 1,008 * 1,000)

有人可以帮我一些计算代码吗?

2 个答案:

答案 0 :(得分:1)

使用@Maurits cumproddplyr::lag

library(dplyr)
df %>% 
   mutate(Adjusted_price = Price * lag(cumprod(Adjustment_factor), k=1, default=0))

     T  Price Adjustment_factor Adjusted_price
1 2010 78.974             1.000        0.00000
2 2012 78.935             1.008       78.93500
3 2013 78.294             1.081       78.92035
4 2014 72.436             1.070       78.92974
5 2015 67.700             1.000       78.93301

答案 1 :(得分:0)

似乎您正在寻找cumprod

在基数R中:

transform(df, Adjusted_price = Price * cumprod(Adjustment_factor))
#     T  Price Adjustment_factor Adjusted_price
#1 2010 78.974             1.000       78.97400
#2 2012 78.935             1.008       79.56648
#3 2013 78.294             1.081       85.31290
#4 2014 72.436             1.070       84.45482
#5 2015 67.700             1.000       78.93301

或者直接写到新列Adjusted_price

df$Adjusted_price <- df$Price * cumprod(df$Adjustment_factor)

或使用tidyverse方式:

library(tidyverse)
df %>%
    arrange(T) %>%
    mutate(Adjusted_price = Price * cumprod(Adjustment_factor))
#     T  Price Adjustment_factor Adjusted_price
#1 2010 78.974             1.000       78.97400
#2 2012 78.935             1.008       79.56648
#3 2013 78.294             1.081       85.31290
#4 2014 72.436             1.070       84.45482
#5 2015 67.700             1.000       78.93301

样本数据

df <- read.table(text =
    "T      Price     Adjustment_factor
2010    78.974     1.000
2012    78.935     1.008
2013    78.294     1.081
2014    72.436     1.070
2015    67.700     1.000", header = T)
相关问题