Count next n rows that meets a condition in R









up vote
0
down vote

favorite












Let's say I have a df that looks like this



ID X_Value
1 40
2 13
3 75
4 83
5 64
6 43
7 74
8 45
9 54
10 84


So what I would like to do, is to do a rolling function that if in the actual and last 4 rows, there are 2 or more values that are higher than X (let's say 70 for this example) then return 1, else 0.



So the output would be something like the following:



ID X_Value Next_4_2
1 40 0
2 13 0
3 75 0
4 83 1
5 64 1
6 43 1
7 24 1
8 45 0
9 74 0
10 84 1


I think this would be possible with a rolling function, but I have tried and not sure how to do it. Thank you in advance










share|improve this question

























    up vote
    0
    down vote

    favorite












    Let's say I have a df that looks like this



    ID X_Value
    1 40
    2 13
    3 75
    4 83
    5 64
    6 43
    7 74
    8 45
    9 54
    10 84


    So what I would like to do, is to do a rolling function that if in the actual and last 4 rows, there are 2 or more values that are higher than X (let's say 70 for this example) then return 1, else 0.



    So the output would be something like the following:



    ID X_Value Next_4_2
    1 40 0
    2 13 0
    3 75 0
    4 83 1
    5 64 1
    6 43 1
    7 24 1
    8 45 0
    9 74 0
    10 84 1


    I think this would be possible with a rolling function, but I have tried and not sure how to do it. Thank you in advance










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Let's say I have a df that looks like this



      ID X_Value
      1 40
      2 13
      3 75
      4 83
      5 64
      6 43
      7 74
      8 45
      9 54
      10 84


      So what I would like to do, is to do a rolling function that if in the actual and last 4 rows, there are 2 or more values that are higher than X (let's say 70 for this example) then return 1, else 0.



      So the output would be something like the following:



      ID X_Value Next_4_2
      1 40 0
      2 13 0
      3 75 0
      4 83 1
      5 64 1
      6 43 1
      7 24 1
      8 45 0
      9 74 0
      10 84 1


      I think this would be possible with a rolling function, but I have tried and not sure how to do it. Thank you in advance










      share|improve this question













      Let's say I have a df that looks like this



      ID X_Value
      1 40
      2 13
      3 75
      4 83
      5 64
      6 43
      7 74
      8 45
      9 54
      10 84


      So what I would like to do, is to do a rolling function that if in the actual and last 4 rows, there are 2 or more values that are higher than X (let's say 70 for this example) then return 1, else 0.



      So the output would be something like the following:



      ID X_Value Next_4_2
      1 40 0
      2 13 0
      3 75 0
      4 83 1
      5 64 1
      6 43 1
      7 24 1
      8 45 0
      9 74 0
      10 84 1


      I think this would be possible with a rolling function, but I have tried and not sure how to do it. Thank you in advance







      r rolling






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 0:41









      Rodrigo Zazueta Donnadieu

      267




      267






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Given your expected output, I suppose you meant "in the actual and previous 3 rows". Then using some rolling function indeed does the job:



          library(zoo)
          thr1 <- 70
          thr2 <- 2
          last <- 3 + 1
          df$Next_4_2 <- 1 * (rollsum(df$X_Value > thr1, last, align = "right", fill = 0) >= thr2)
          df
          # ID X_Value Next_4_2
          # 1 1 40 0
          # 2 2 13 0
          # 3 3 75 0
          # 4 4 83 1
          # 5 5 64 1
          # 6 6 43 1
          # 7 7 74 1
          # 8 8 45 0
          # 9 9 54 0
          # 10 10 84 1





          share|improve this answer



























            up vote
            0
            down vote













            The indexing using max(1,i-3) is perhaps the only part of the code worth remembering. I might help in subsequent construction when a for-loop was really needed.



            dat$X_Next_4_2 <- integer( length(dat$X_Value) )
            dat$ X_Next_4_2[1]=0
            for (i in 2:length(dat$X_Value) )
            dat$ X_Next_4_2[i]=
            ( sum(dat$X_Value[i: (max(0, i-4) )] >=70) >=2 )


            (Not very pretty and clearly inferior to the rollsum answer already posted.)






            share|improve this answer




















              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%2f53235007%2fcount-next-n-rows-that-meets-a-condition-in-r%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote



              accepted










              Given your expected output, I suppose you meant "in the actual and previous 3 rows". Then using some rolling function indeed does the job:



              library(zoo)
              thr1 <- 70
              thr2 <- 2
              last <- 3 + 1
              df$Next_4_2 <- 1 * (rollsum(df$X_Value > thr1, last, align = "right", fill = 0) >= thr2)
              df
              # ID X_Value Next_4_2
              # 1 1 40 0
              # 2 2 13 0
              # 3 3 75 0
              # 4 4 83 1
              # 5 5 64 1
              # 6 6 43 1
              # 7 7 74 1
              # 8 8 45 0
              # 9 9 54 0
              # 10 10 84 1





              share|improve this answer
























                up vote
                3
                down vote



                accepted










                Given your expected output, I suppose you meant "in the actual and previous 3 rows". Then using some rolling function indeed does the job:



                library(zoo)
                thr1 <- 70
                thr2 <- 2
                last <- 3 + 1
                df$Next_4_2 <- 1 * (rollsum(df$X_Value > thr1, last, align = "right", fill = 0) >= thr2)
                df
                # ID X_Value Next_4_2
                # 1 1 40 0
                # 2 2 13 0
                # 3 3 75 0
                # 4 4 83 1
                # 5 5 64 1
                # 6 6 43 1
                # 7 7 74 1
                # 8 8 45 0
                # 9 9 54 0
                # 10 10 84 1





                share|improve this answer






















                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  Given your expected output, I suppose you meant "in the actual and previous 3 rows". Then using some rolling function indeed does the job:



                  library(zoo)
                  thr1 <- 70
                  thr2 <- 2
                  last <- 3 + 1
                  df$Next_4_2 <- 1 * (rollsum(df$X_Value > thr1, last, align = "right", fill = 0) >= thr2)
                  df
                  # ID X_Value Next_4_2
                  # 1 1 40 0
                  # 2 2 13 0
                  # 3 3 75 0
                  # 4 4 83 1
                  # 5 5 64 1
                  # 6 6 43 1
                  # 7 7 74 1
                  # 8 8 45 0
                  # 9 9 54 0
                  # 10 10 84 1





                  share|improve this answer












                  Given your expected output, I suppose you meant "in the actual and previous 3 rows". Then using some rolling function indeed does the job:



                  library(zoo)
                  thr1 <- 70
                  thr2 <- 2
                  last <- 3 + 1
                  df$Next_4_2 <- 1 * (rollsum(df$X_Value > thr1, last, align = "right", fill = 0) >= thr2)
                  df
                  # ID X_Value Next_4_2
                  # 1 1 40 0
                  # 2 2 13 0
                  # 3 3 75 0
                  # 4 4 83 1
                  # 5 5 64 1
                  # 6 6 43 1
                  # 7 7 74 1
                  # 8 8 45 0
                  # 9 9 54 0
                  # 10 10 84 1






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 0:51









                  Julius Vainora

                  27.6k75878




                  27.6k75878






















                      up vote
                      0
                      down vote













                      The indexing using max(1,i-3) is perhaps the only part of the code worth remembering. I might help in subsequent construction when a for-loop was really needed.



                      dat$X_Next_4_2 <- integer( length(dat$X_Value) )
                      dat$ X_Next_4_2[1]=0
                      for (i in 2:length(dat$X_Value) )
                      dat$ X_Next_4_2[i]=
                      ( sum(dat$X_Value[i: (max(0, i-4) )] >=70) >=2 )


                      (Not very pretty and clearly inferior to the rollsum answer already posted.)






                      share|improve this answer
























                        up vote
                        0
                        down vote













                        The indexing using max(1,i-3) is perhaps the only part of the code worth remembering. I might help in subsequent construction when a for-loop was really needed.



                        dat$X_Next_4_2 <- integer( length(dat$X_Value) )
                        dat$ X_Next_4_2[1]=0
                        for (i in 2:length(dat$X_Value) )
                        dat$ X_Next_4_2[i]=
                        ( sum(dat$X_Value[i: (max(0, i-4) )] >=70) >=2 )


                        (Not very pretty and clearly inferior to the rollsum answer already posted.)






                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          The indexing using max(1,i-3) is perhaps the only part of the code worth remembering. I might help in subsequent construction when a for-loop was really needed.



                          dat$X_Next_4_2 <- integer( length(dat$X_Value) )
                          dat$ X_Next_4_2[1]=0
                          for (i in 2:length(dat$X_Value) )
                          dat$ X_Next_4_2[i]=
                          ( sum(dat$X_Value[i: (max(0, i-4) )] >=70) >=2 )


                          (Not very pretty and clearly inferior to the rollsum answer already posted.)






                          share|improve this answer












                          The indexing using max(1,i-3) is perhaps the only part of the code worth remembering. I might help in subsequent construction when a for-loop was really needed.



                          dat$X_Next_4_2 <- integer( length(dat$X_Value) )
                          dat$ X_Next_4_2[1]=0
                          for (i in 2:length(dat$X_Value) )
                          dat$ X_Next_4_2[i]=
                          ( sum(dat$X_Value[i: (max(0, i-4) )] >=70) >=2 )


                          (Not very pretty and clearly inferior to the rollsum answer already posted.)







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 10 at 1:05









                          42-

                          210k14248392




                          210k14248392



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235007%2fcount-next-n-rows-that-meets-a-condition-in-r%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

                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                              Syphilis

                              Darth Vader #20