dplyr Rolling Conditional Counts











up vote
0
down vote

favorite












I have a data frame as follows:



df <- data.frame(
Item=c("A","A","A","A","A","B","B","B","B","B"),
Date=c("2018-1-1","2018-2-1","2018-3-1","2018-4-1","2018-5-1","2018-1-1","2018-2-1",
"2018-3-1","2018-4-1","2018-5-1"),
Value=rnorm(10))


I want to mutate a new column grouped by Item, to count the number of values higher than 0 within the window of 3 (or any other integer I specify).



I am familiar with tidyverse, therefore, a dplyr solution would be most welcome.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a data frame as follows:



    df <- data.frame(
    Item=c("A","A","A","A","A","B","B","B","B","B"),
    Date=c("2018-1-1","2018-2-1","2018-3-1","2018-4-1","2018-5-1","2018-1-1","2018-2-1",
    "2018-3-1","2018-4-1","2018-5-1"),
    Value=rnorm(10))


    I want to mutate a new column grouped by Item, to count the number of values higher than 0 within the window of 3 (or any other integer I specify).



    I am familiar with tidyverse, therefore, a dplyr solution would be most welcome.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a data frame as follows:



      df <- data.frame(
      Item=c("A","A","A","A","A","B","B","B","B","B"),
      Date=c("2018-1-1","2018-2-1","2018-3-1","2018-4-1","2018-5-1","2018-1-1","2018-2-1",
      "2018-3-1","2018-4-1","2018-5-1"),
      Value=rnorm(10))


      I want to mutate a new column grouped by Item, to count the number of values higher than 0 within the window of 3 (or any other integer I specify).



      I am familiar with tidyverse, therefore, a dplyr solution would be most welcome.










      share|improve this question















      I have a data frame as follows:



      df <- data.frame(
      Item=c("A","A","A","A","A","B","B","B","B","B"),
      Date=c("2018-1-1","2018-2-1","2018-3-1","2018-4-1","2018-5-1","2018-1-1","2018-2-1",
      "2018-3-1","2018-4-1","2018-5-1"),
      Value=rnorm(10))


      I want to mutate a new column grouped by Item, to count the number of values higher than 0 within the window of 3 (or any other integer I specify).



      I am familiar with tidyverse, therefore, a dplyr solution would be most welcome.







      r dplyr






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 13:47









      Ronak Shah

      30.7k103753




      30.7k103753










      asked Nov 20 at 12:51









      Felix Zhao

      4614




      4614
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










            Item  Date       Value
          <fct> <date> <int>
          1 A 2018-01-01 3
          2 B 2018-01-01 2
          3 B 2018-02-01 -5
          4 A 2018-02-01 -3
          5 A 2018-03-01 4
          6 B 2018-03-01 -2
          7 A 2018-04-01 5
          8 B 2018-04-01 0
          9 A 2018-05-01 1
          10 B 2018-05-01 -4


          Changed rnorm example for clarity, used sample(-5:5):



          > df <- df %>% mutate(greater_than = (Value>0)*Value) %>%
          group_by(Item) %>% arrange(Date) %>% mutate(greater_than =
          zoo::rollapplyr(greater_than, 3, sum, partial = T))
          df %>% arrange(Item) %>% head(10)


          Should look like this:



           1 A     2018-01-01     3            3
          2 A 2018-02-01 -3 3
          3 A 2018-03-01 4 7
          4 A 2018-04-01 5 9
          5 A 2018-05-01 1 10
          6 B 2018-01-01 2 2
          7 B 2018-02-01 -5 2
          8 B 2018-03-01 -2 2
          9 B 2018-04-01 0 0
          10 B 2018-05-01 -4 0





          share|improve this answer




























            up vote
            3
            down vote













            Think zoo:: package if you want to roll anything.



            df$new<-
            zoo::rollsum( df$Value > 0, 3, fill = NA )

            # Item Date Value new
            #1 A 2018-1-1 0.5852699 NA
            #2 A 2018-2-1 -0.7383377 1
            #3 A 2018-3-1 -0.3157693 1
            #4 A 2018-4-1 1.2475237 1
            #5 A 2018-5-1 -1.5479757 1
            #6 B 2018-1-1 -0.6913331 0
            #7 B 2018-2-1 -0.2423809 0
            #8 B 2018-3-1 -1.6363024 0
            #9 B 2018-4-1 -0.3256263 1
            #10 B 2018-5-1 0.3563144 NA


            You have an option of the "window-position". Have a closer look at argument align = c("center", "left", "right").





            So as a dplyr chain:



            df %>% group_by(Item) %>% dplyr::mutate( new = zoo::rollsum( Value > 0, 3, fill = NA ))





            share|improve this answer























            • Thanks Andre for your help. I tested with your method, it works!
              – Felix Zhao
              Nov 21 at 12:04










            • If your problem got solved please choose an answer.
              – Andre Elrico
              Nov 22 at 11:11


















            up vote
            1
            down vote













            You could use the RcppRoll package.



            require(RcppRoll)
            df$new <- df$new <- RcppRoll::roll_sum(df$Value > 0, 3, fill = NA)


            Using Tidyverse:



            df %>% 
            group_by(Item) %>%
            dplyr::mutate(new = RcppRoll::roll_sum(Value > 0, 3, fill = NA))


            Speedwise this is faster than the zoo Package:



            n <- 10000
            df <- data.frame(
            Item = sample(LETTERS, n, replace = TRUE),
            Value = rnorm(n))

            df_grouped <- df %>%
            group_by(Item)
            microbenchmark::microbenchmark(
            RcppRoll = df_grouped <- df_grouped %>% dplyr::mutate(new_RcppRoll = RcppRoll::roll_sum(Value > 0, 3, fill = NA)),
            zoo = df_grouped <- df_grouped %>% dplyr::mutate(new_zoo = zoo::rollsum( Value > 0, 3, fill = NA ))
            )


            Results in:



            Unit: milliseconds
            expr min lq mean median uq max neval
            RcppRoll 2.509003 2.741993 2.929227 2.83913 2.983726 5.832962 100
            zoo 11.172920 11.785113 13.288970 12.43320 13.607826 25.879754 100


            And



            all.equal(df_grouped$new_RcppRoll, df_grouped$new_zoo)
            TRUE





            share|improve this answer





















            • Hi Rentrop, it took me to figure out the alignment clause and it is a really good package dealing with rolling. Thanks a lot for your help!
              – Felix Zhao
              Nov 21 at 12:06











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393406%2fdplyr-rolling-conditional-counts%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










              Item  Date       Value
            <fct> <date> <int>
            1 A 2018-01-01 3
            2 B 2018-01-01 2
            3 B 2018-02-01 -5
            4 A 2018-02-01 -3
            5 A 2018-03-01 4
            6 B 2018-03-01 -2
            7 A 2018-04-01 5
            8 B 2018-04-01 0
            9 A 2018-05-01 1
            10 B 2018-05-01 -4


            Changed rnorm example for clarity, used sample(-5:5):



            > df <- df %>% mutate(greater_than = (Value>0)*Value) %>%
            group_by(Item) %>% arrange(Date) %>% mutate(greater_than =
            zoo::rollapplyr(greater_than, 3, sum, partial = T))
            df %>% arrange(Item) %>% head(10)


            Should look like this:



             1 A     2018-01-01     3            3
            2 A 2018-02-01 -3 3
            3 A 2018-03-01 4 7
            4 A 2018-04-01 5 9
            5 A 2018-05-01 1 10
            6 B 2018-01-01 2 2
            7 B 2018-02-01 -5 2
            8 B 2018-03-01 -2 2
            9 B 2018-04-01 0 0
            10 B 2018-05-01 -4 0





            share|improve this answer

























              up vote
              0
              down vote



              accepted










                Item  Date       Value
              <fct> <date> <int>
              1 A 2018-01-01 3
              2 B 2018-01-01 2
              3 B 2018-02-01 -5
              4 A 2018-02-01 -3
              5 A 2018-03-01 4
              6 B 2018-03-01 -2
              7 A 2018-04-01 5
              8 B 2018-04-01 0
              9 A 2018-05-01 1
              10 B 2018-05-01 -4


              Changed rnorm example for clarity, used sample(-5:5):



              > df <- df %>% mutate(greater_than = (Value>0)*Value) %>%
              group_by(Item) %>% arrange(Date) %>% mutate(greater_than =
              zoo::rollapplyr(greater_than, 3, sum, partial = T))
              df %>% arrange(Item) %>% head(10)


              Should look like this:



               1 A     2018-01-01     3            3
              2 A 2018-02-01 -3 3
              3 A 2018-03-01 4 7
              4 A 2018-04-01 5 9
              5 A 2018-05-01 1 10
              6 B 2018-01-01 2 2
              7 B 2018-02-01 -5 2
              8 B 2018-03-01 -2 2
              9 B 2018-04-01 0 0
              10 B 2018-05-01 -4 0





              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                  Item  Date       Value
                <fct> <date> <int>
                1 A 2018-01-01 3
                2 B 2018-01-01 2
                3 B 2018-02-01 -5
                4 A 2018-02-01 -3
                5 A 2018-03-01 4
                6 B 2018-03-01 -2
                7 A 2018-04-01 5
                8 B 2018-04-01 0
                9 A 2018-05-01 1
                10 B 2018-05-01 -4


                Changed rnorm example for clarity, used sample(-5:5):



                > df <- df %>% mutate(greater_than = (Value>0)*Value) %>%
                group_by(Item) %>% arrange(Date) %>% mutate(greater_than =
                zoo::rollapplyr(greater_than, 3, sum, partial = T))
                df %>% arrange(Item) %>% head(10)


                Should look like this:



                 1 A     2018-01-01     3            3
                2 A 2018-02-01 -3 3
                3 A 2018-03-01 4 7
                4 A 2018-04-01 5 9
                5 A 2018-05-01 1 10
                6 B 2018-01-01 2 2
                7 B 2018-02-01 -5 2
                8 B 2018-03-01 -2 2
                9 B 2018-04-01 0 0
                10 B 2018-05-01 -4 0





                share|improve this answer












                  Item  Date       Value
                <fct> <date> <int>
                1 A 2018-01-01 3
                2 B 2018-01-01 2
                3 B 2018-02-01 -5
                4 A 2018-02-01 -3
                5 A 2018-03-01 4
                6 B 2018-03-01 -2
                7 A 2018-04-01 5
                8 B 2018-04-01 0
                9 A 2018-05-01 1
                10 B 2018-05-01 -4


                Changed rnorm example for clarity, used sample(-5:5):



                > df <- df %>% mutate(greater_than = (Value>0)*Value) %>%
                group_by(Item) %>% arrange(Date) %>% mutate(greater_than =
                zoo::rollapplyr(greater_than, 3, sum, partial = T))
                df %>% arrange(Item) %>% head(10)


                Should look like this:



                 1 A     2018-01-01     3            3
                2 A 2018-02-01 -3 3
                3 A 2018-03-01 4 7
                4 A 2018-04-01 5 9
                5 A 2018-05-01 1 10
                6 B 2018-01-01 2 2
                7 B 2018-02-01 -5 2
                8 B 2018-03-01 -2 2
                9 B 2018-04-01 0 0
                10 B 2018-05-01 -4 0






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 at 13:37









                Matheus Deister Veiga

                16




                16
























                    up vote
                    3
                    down vote













                    Think zoo:: package if you want to roll anything.



                    df$new<-
                    zoo::rollsum( df$Value > 0, 3, fill = NA )

                    # Item Date Value new
                    #1 A 2018-1-1 0.5852699 NA
                    #2 A 2018-2-1 -0.7383377 1
                    #3 A 2018-3-1 -0.3157693 1
                    #4 A 2018-4-1 1.2475237 1
                    #5 A 2018-5-1 -1.5479757 1
                    #6 B 2018-1-1 -0.6913331 0
                    #7 B 2018-2-1 -0.2423809 0
                    #8 B 2018-3-1 -1.6363024 0
                    #9 B 2018-4-1 -0.3256263 1
                    #10 B 2018-5-1 0.3563144 NA


                    You have an option of the "window-position". Have a closer look at argument align = c("center", "left", "right").





                    So as a dplyr chain:



                    df %>% group_by(Item) %>% dplyr::mutate( new = zoo::rollsum( Value > 0, 3, fill = NA ))





                    share|improve this answer























                    • Thanks Andre for your help. I tested with your method, it works!
                      – Felix Zhao
                      Nov 21 at 12:04










                    • If your problem got solved please choose an answer.
                      – Andre Elrico
                      Nov 22 at 11:11















                    up vote
                    3
                    down vote













                    Think zoo:: package if you want to roll anything.



                    df$new<-
                    zoo::rollsum( df$Value > 0, 3, fill = NA )

                    # Item Date Value new
                    #1 A 2018-1-1 0.5852699 NA
                    #2 A 2018-2-1 -0.7383377 1
                    #3 A 2018-3-1 -0.3157693 1
                    #4 A 2018-4-1 1.2475237 1
                    #5 A 2018-5-1 -1.5479757 1
                    #6 B 2018-1-1 -0.6913331 0
                    #7 B 2018-2-1 -0.2423809 0
                    #8 B 2018-3-1 -1.6363024 0
                    #9 B 2018-4-1 -0.3256263 1
                    #10 B 2018-5-1 0.3563144 NA


                    You have an option of the "window-position". Have a closer look at argument align = c("center", "left", "right").





                    So as a dplyr chain:



                    df %>% group_by(Item) %>% dplyr::mutate( new = zoo::rollsum( Value > 0, 3, fill = NA ))





                    share|improve this answer























                    • Thanks Andre for your help. I tested with your method, it works!
                      – Felix Zhao
                      Nov 21 at 12:04










                    • If your problem got solved please choose an answer.
                      – Andre Elrico
                      Nov 22 at 11:11













                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    Think zoo:: package if you want to roll anything.



                    df$new<-
                    zoo::rollsum( df$Value > 0, 3, fill = NA )

                    # Item Date Value new
                    #1 A 2018-1-1 0.5852699 NA
                    #2 A 2018-2-1 -0.7383377 1
                    #3 A 2018-3-1 -0.3157693 1
                    #4 A 2018-4-1 1.2475237 1
                    #5 A 2018-5-1 -1.5479757 1
                    #6 B 2018-1-1 -0.6913331 0
                    #7 B 2018-2-1 -0.2423809 0
                    #8 B 2018-3-1 -1.6363024 0
                    #9 B 2018-4-1 -0.3256263 1
                    #10 B 2018-5-1 0.3563144 NA


                    You have an option of the "window-position". Have a closer look at argument align = c("center", "left", "right").





                    So as a dplyr chain:



                    df %>% group_by(Item) %>% dplyr::mutate( new = zoo::rollsum( Value > 0, 3, fill = NA ))





                    share|improve this answer














                    Think zoo:: package if you want to roll anything.



                    df$new<-
                    zoo::rollsum( df$Value > 0, 3, fill = NA )

                    # Item Date Value new
                    #1 A 2018-1-1 0.5852699 NA
                    #2 A 2018-2-1 -0.7383377 1
                    #3 A 2018-3-1 -0.3157693 1
                    #4 A 2018-4-1 1.2475237 1
                    #5 A 2018-5-1 -1.5479757 1
                    #6 B 2018-1-1 -0.6913331 0
                    #7 B 2018-2-1 -0.2423809 0
                    #8 B 2018-3-1 -1.6363024 0
                    #9 B 2018-4-1 -0.3256263 1
                    #10 B 2018-5-1 0.3563144 NA


                    You have an option of the "window-position". Have a closer look at argument align = c("center", "left", "right").





                    So as a dplyr chain:



                    df %>% group_by(Item) %>% dplyr::mutate( new = zoo::rollsum( Value > 0, 3, fill = NA ))






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 20 at 13:16

























                    answered Nov 20 at 13:03









                    Andre Elrico

                    5,56311027




                    5,56311027












                    • Thanks Andre for your help. I tested with your method, it works!
                      – Felix Zhao
                      Nov 21 at 12:04










                    • If your problem got solved please choose an answer.
                      – Andre Elrico
                      Nov 22 at 11:11


















                    • Thanks Andre for your help. I tested with your method, it works!
                      – Felix Zhao
                      Nov 21 at 12:04










                    • If your problem got solved please choose an answer.
                      – Andre Elrico
                      Nov 22 at 11:11
















                    Thanks Andre for your help. I tested with your method, it works!
                    – Felix Zhao
                    Nov 21 at 12:04




                    Thanks Andre for your help. I tested with your method, it works!
                    – Felix Zhao
                    Nov 21 at 12:04












                    If your problem got solved please choose an answer.
                    – Andre Elrico
                    Nov 22 at 11:11




                    If your problem got solved please choose an answer.
                    – Andre Elrico
                    Nov 22 at 11:11










                    up vote
                    1
                    down vote













                    You could use the RcppRoll package.



                    require(RcppRoll)
                    df$new <- df$new <- RcppRoll::roll_sum(df$Value > 0, 3, fill = NA)


                    Using Tidyverse:



                    df %>% 
                    group_by(Item) %>%
                    dplyr::mutate(new = RcppRoll::roll_sum(Value > 0, 3, fill = NA))


                    Speedwise this is faster than the zoo Package:



                    n <- 10000
                    df <- data.frame(
                    Item = sample(LETTERS, n, replace = TRUE),
                    Value = rnorm(n))

                    df_grouped <- df %>%
                    group_by(Item)
                    microbenchmark::microbenchmark(
                    RcppRoll = df_grouped <- df_grouped %>% dplyr::mutate(new_RcppRoll = RcppRoll::roll_sum(Value > 0, 3, fill = NA)),
                    zoo = df_grouped <- df_grouped %>% dplyr::mutate(new_zoo = zoo::rollsum( Value > 0, 3, fill = NA ))
                    )


                    Results in:



                    Unit: milliseconds
                    expr min lq mean median uq max neval
                    RcppRoll 2.509003 2.741993 2.929227 2.83913 2.983726 5.832962 100
                    zoo 11.172920 11.785113 13.288970 12.43320 13.607826 25.879754 100


                    And



                    all.equal(df_grouped$new_RcppRoll, df_grouped$new_zoo)
                    TRUE





                    share|improve this answer





















                    • Hi Rentrop, it took me to figure out the alignment clause and it is a really good package dealing with rolling. Thanks a lot for your help!
                      – Felix Zhao
                      Nov 21 at 12:06















                    up vote
                    1
                    down vote













                    You could use the RcppRoll package.



                    require(RcppRoll)
                    df$new <- df$new <- RcppRoll::roll_sum(df$Value > 0, 3, fill = NA)


                    Using Tidyverse:



                    df %>% 
                    group_by(Item) %>%
                    dplyr::mutate(new = RcppRoll::roll_sum(Value > 0, 3, fill = NA))


                    Speedwise this is faster than the zoo Package:



                    n <- 10000
                    df <- data.frame(
                    Item = sample(LETTERS, n, replace = TRUE),
                    Value = rnorm(n))

                    df_grouped <- df %>%
                    group_by(Item)
                    microbenchmark::microbenchmark(
                    RcppRoll = df_grouped <- df_grouped %>% dplyr::mutate(new_RcppRoll = RcppRoll::roll_sum(Value > 0, 3, fill = NA)),
                    zoo = df_grouped <- df_grouped %>% dplyr::mutate(new_zoo = zoo::rollsum( Value > 0, 3, fill = NA ))
                    )


                    Results in:



                    Unit: milliseconds
                    expr min lq mean median uq max neval
                    RcppRoll 2.509003 2.741993 2.929227 2.83913 2.983726 5.832962 100
                    zoo 11.172920 11.785113 13.288970 12.43320 13.607826 25.879754 100


                    And



                    all.equal(df_grouped$new_RcppRoll, df_grouped$new_zoo)
                    TRUE





                    share|improve this answer





















                    • Hi Rentrop, it took me to figure out the alignment clause and it is a really good package dealing with rolling. Thanks a lot for your help!
                      – Felix Zhao
                      Nov 21 at 12:06













                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    You could use the RcppRoll package.



                    require(RcppRoll)
                    df$new <- df$new <- RcppRoll::roll_sum(df$Value > 0, 3, fill = NA)


                    Using Tidyverse:



                    df %>% 
                    group_by(Item) %>%
                    dplyr::mutate(new = RcppRoll::roll_sum(Value > 0, 3, fill = NA))


                    Speedwise this is faster than the zoo Package:



                    n <- 10000
                    df <- data.frame(
                    Item = sample(LETTERS, n, replace = TRUE),
                    Value = rnorm(n))

                    df_grouped <- df %>%
                    group_by(Item)
                    microbenchmark::microbenchmark(
                    RcppRoll = df_grouped <- df_grouped %>% dplyr::mutate(new_RcppRoll = RcppRoll::roll_sum(Value > 0, 3, fill = NA)),
                    zoo = df_grouped <- df_grouped %>% dplyr::mutate(new_zoo = zoo::rollsum( Value > 0, 3, fill = NA ))
                    )


                    Results in:



                    Unit: milliseconds
                    expr min lq mean median uq max neval
                    RcppRoll 2.509003 2.741993 2.929227 2.83913 2.983726 5.832962 100
                    zoo 11.172920 11.785113 13.288970 12.43320 13.607826 25.879754 100


                    And



                    all.equal(df_grouped$new_RcppRoll, df_grouped$new_zoo)
                    TRUE





                    share|improve this answer












                    You could use the RcppRoll package.



                    require(RcppRoll)
                    df$new <- df$new <- RcppRoll::roll_sum(df$Value > 0, 3, fill = NA)


                    Using Tidyverse:



                    df %>% 
                    group_by(Item) %>%
                    dplyr::mutate(new = RcppRoll::roll_sum(Value > 0, 3, fill = NA))


                    Speedwise this is faster than the zoo Package:



                    n <- 10000
                    df <- data.frame(
                    Item = sample(LETTERS, n, replace = TRUE),
                    Value = rnorm(n))

                    df_grouped <- df %>%
                    group_by(Item)
                    microbenchmark::microbenchmark(
                    RcppRoll = df_grouped <- df_grouped %>% dplyr::mutate(new_RcppRoll = RcppRoll::roll_sum(Value > 0, 3, fill = NA)),
                    zoo = df_grouped <- df_grouped %>% dplyr::mutate(new_zoo = zoo::rollsum( Value > 0, 3, fill = NA ))
                    )


                    Results in:



                    Unit: milliseconds
                    expr min lq mean median uq max neval
                    RcppRoll 2.509003 2.741993 2.929227 2.83913 2.983726 5.832962 100
                    zoo 11.172920 11.785113 13.288970 12.43320 13.607826 25.879754 100


                    And



                    all.equal(df_grouped$new_RcppRoll, df_grouped$new_zoo)
                    TRUE






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 at 13:33









                    Rentrop

                    13.9k33871




                    13.9k33871












                    • Hi Rentrop, it took me to figure out the alignment clause and it is a really good package dealing with rolling. Thanks a lot for your help!
                      – Felix Zhao
                      Nov 21 at 12:06


















                    • Hi Rentrop, it took me to figure out the alignment clause and it is a really good package dealing with rolling. Thanks a lot for your help!
                      – Felix Zhao
                      Nov 21 at 12:06
















                    Hi Rentrop, it took me to figure out the alignment clause and it is a really good package dealing with rolling. Thanks a lot for your help!
                    – Felix Zhao
                    Nov 21 at 12:06




                    Hi Rentrop, it took me to figure out the alignment clause and it is a really good package dealing with rolling. Thanks a lot for your help!
                    – Felix Zhao
                    Nov 21 at 12:06


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393406%2fdplyr-rolling-conditional-counts%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen