Conditional Cumulative Sum in R

后端 未结 3 1675
无人共我
无人共我 2021-01-02 06:34

I have a time series data frame and want to compute cumulative returns for stock symbols intra-day for a range of dates. When the symbol and/or date changes the cumulative

3条回答
  •  盖世英雄少女心
    2021-01-02 07:12

    Sample data (note: I used lubridate library just to call the dmy function)

    library(lubridate) 
    df <- data.frame(
      Date = dmy( c( "1/2/2013", "1/2/2013", "1/2/2013", "1/2/2013"
                     , "1/2/2013", "1/2/2013", "1/3/2013", "1/3/2013", "1/3/2013" ) ),
      Symbol = c( "AA", "AA", "AA", "AAPL", "AAPL", "AAPL", "AA", "AA", "AA" ),
      Return = c( NA, 1.19, 0.89, NA, 0.22, 0.21, NA, -1.80, -0.52 )
    )
    

    Now, using dplyr, you can group_by your dataframe and create the desired column Cum_Sum:

    library(dplyr)
    df %>% group_by(Date, Symbol) %>% 
      mutate( Return_aux = ifelse( is.na(Return), 0, Return ), #remove NA
              Cum_Sum = cumsum(Return_aux) )
    
    # A tibble: 9 x 5
    # Groups:   Date, Symbol [3]
      Date       Symbol Return Return_aux Cum_Sum
                       
    1 2013-02-01 AA      NA          0       0   
    2 2013-02-01 AA       1.19       1.19    1.19
    3 2013-02-01 AA       0.89       0.89    2.08
    4 2013-02-01 AAPL    NA          0       0   
    5 2013-02-01 AAPL     0.22       0.22    0.22
    6 2013-02-01 AAPL     0.21       0.21    0.43
    7 2013-03-01 AA      NA          0       0   
    8 2013-03-01 AA      -1.8       -1.8    -1.8 
    9 2013-03-01 AA      -0.52      -0.52   -2.32
    

提交回复
热议问题