R Cumulative Sum with a condition and a reset

后端 未结 3 566
日久生厌
日久生厌 2020-12-17 04:48

I have a signal position indicator vector consisting out of -1s and 1s. In addition, I have volume data which I want to sum based on the value of Signal. The basic data tabl

3条回答
  •  独厮守ぢ
    2020-12-17 05:52

    A pure dplyr way would be:

    df %>% 
      na.omit() %>% # omit NA to not multiply by NA
      mutate(isStep = (Signal - lag(Signal, 1)) != 0) %>% # Create a dummy variable for steps 
      mutate(isStep = ifelse(is.na(isStep), FALSE, isStep)) %>% 
      mutate(grp = cumsum(isStep)) %>% # create new ID based on steps
      group_by(grp) %>%  # group by before created steps
      mutate(res = cumsum(Signal * Volume)) %>% # calculate value
      select(x, Signal, Volume, res)
    
    # # A tibble: 19 x 5
    # # Groups:   grp [6]
    #      grp          x Signal    Volume        res
    #                      
    #  1     0 2016-01-05     -1  23258238  -23258238
    #  2     0 2016-01-06     -1  25096183  -48354421
    #  3     0 2016-01-07     -1  45172906  -93527327
    #  4     0 2016-01-08     -1  35402298 -128929625
    #  5     0 2016-01-11     -1  29932385 -158862010
    #  6     0 2016-01-12     -1  28395390 -187257400
    #  7     0 2016-01-13     -1  33410553 -220667953
    #  8     0 2016-01-14     -1  48658623 -269326576
    #  9     1 2016-01-15      1  46132781   46132781
    # 10     1 2016-01-19      1  30998256   77131037
    # 11     2 2016-01-20     -1  59051429  -59051429
    # 12     3 2016-01-21      1  30518939   30518939
    # 13     3 2016-01-22      1  30495387   61014326
    # 14     3 2016-01-25      1  32482015   93496341
    # 15     4 2016-01-26     -1  26877080  -26877080
    # 16     4 2016-01-27     -1  58699359  -85576439
    # 17     5 2016-01-28      1 107475327  107475327
    # 18     5 2016-01-29      1  62739548  170214875
    # 19     5 2016-02-01      1  46132726  216347601
    

提交回复
热议问题