Count number of values which are less than current value












3















I'd like to count the rows in the column input if the values are smaller than the current row (Please see the results wanted below). The issue to me is that the condition is based on current row value, so it is very different from general case where the condition is a fixed number.



data <- data.frame(input = c(1,1,1,1,2,2,3,5,5,5,5,6))

input
1 1
2 1
3 1
4 1
5 2
6 2
7 3
8 5
9 5
10 5
11 5
12 6


The results I expect to get are like this. For example, for observations 5 and 6 (with value 2), there are 4 observations with value 1 less than their value 2. Hence count is given value 4.



    input count
1 1 0
2 1 0
3 1 0
4 1 0
5 2 4
6 2 4
7 3 6
8 5 7
9 5 7
10 5 7
11 5 7
12 6 11




Edit: as I am dealing with grouped data with dplyr, the ultimate results I wish to get is like below, that is, I am wishing the conditions could be dynamic within each group.



data <- data.frame(id = c(1,1,2,2,2,3,3,4,4,4,4,4), 
input = c(1,1,1,1,2,2,3,5,5,5,5,6),
count=c(0,0,0,0,2,0,1,0,0,0,0,4))

id input count
1 1 1 0
2 1 1 0
3 2 1 0
4 2 1 0
5 2 2 2
6 3 2 0
7 3 3 1
8 4 5 0
9 4 5 0
10 4 5 0
11 4 5 0
12 4 6 4









share|improve this question





























    3















    I'd like to count the rows in the column input if the values are smaller than the current row (Please see the results wanted below). The issue to me is that the condition is based on current row value, so it is very different from general case where the condition is a fixed number.



    data <- data.frame(input = c(1,1,1,1,2,2,3,5,5,5,5,6))

    input
    1 1
    2 1
    3 1
    4 1
    5 2
    6 2
    7 3
    8 5
    9 5
    10 5
    11 5
    12 6


    The results I expect to get are like this. For example, for observations 5 and 6 (with value 2), there are 4 observations with value 1 less than their value 2. Hence count is given value 4.



        input count
    1 1 0
    2 1 0
    3 1 0
    4 1 0
    5 2 4
    6 2 4
    7 3 6
    8 5 7
    9 5 7
    10 5 7
    11 5 7
    12 6 11




    Edit: as I am dealing with grouped data with dplyr, the ultimate results I wish to get is like below, that is, I am wishing the conditions could be dynamic within each group.



    data <- data.frame(id = c(1,1,2,2,2,3,3,4,4,4,4,4), 
    input = c(1,1,1,1,2,2,3,5,5,5,5,6),
    count=c(0,0,0,0,2,0,1,0,0,0,0,4))

    id input count
    1 1 1 0
    2 1 1 0
    3 2 1 0
    4 2 1 0
    5 2 2 2
    6 3 2 0
    7 3 3 1
    8 4 5 0
    9 4 5 0
    10 4 5 0
    11 4 5 0
    12 4 6 4









    share|improve this question



























      3












      3








      3








      I'd like to count the rows in the column input if the values are smaller than the current row (Please see the results wanted below). The issue to me is that the condition is based on current row value, so it is very different from general case where the condition is a fixed number.



      data <- data.frame(input = c(1,1,1,1,2,2,3,5,5,5,5,6))

      input
      1 1
      2 1
      3 1
      4 1
      5 2
      6 2
      7 3
      8 5
      9 5
      10 5
      11 5
      12 6


      The results I expect to get are like this. For example, for observations 5 and 6 (with value 2), there are 4 observations with value 1 less than their value 2. Hence count is given value 4.



          input count
      1 1 0
      2 1 0
      3 1 0
      4 1 0
      5 2 4
      6 2 4
      7 3 6
      8 5 7
      9 5 7
      10 5 7
      11 5 7
      12 6 11




      Edit: as I am dealing with grouped data with dplyr, the ultimate results I wish to get is like below, that is, I am wishing the conditions could be dynamic within each group.



      data <- data.frame(id = c(1,1,2,2,2,3,3,4,4,4,4,4), 
      input = c(1,1,1,1,2,2,3,5,5,5,5,6),
      count=c(0,0,0,0,2,0,1,0,0,0,0,4))

      id input count
      1 1 1 0
      2 1 1 0
      3 2 1 0
      4 2 1 0
      5 2 2 2
      6 3 2 0
      7 3 3 1
      8 4 5 0
      9 4 5 0
      10 4 5 0
      11 4 5 0
      12 4 6 4









      share|improve this question
















      I'd like to count the rows in the column input if the values are smaller than the current row (Please see the results wanted below). The issue to me is that the condition is based on current row value, so it is very different from general case where the condition is a fixed number.



      data <- data.frame(input = c(1,1,1,1,2,2,3,5,5,5,5,6))

      input
      1 1
      2 1
      3 1
      4 1
      5 2
      6 2
      7 3
      8 5
      9 5
      10 5
      11 5
      12 6


      The results I expect to get are like this. For example, for observations 5 and 6 (with value 2), there are 4 observations with value 1 less than their value 2. Hence count is given value 4.



          input count
      1 1 0
      2 1 0
      3 1 0
      4 1 0
      5 2 4
      6 2 4
      7 3 6
      8 5 7
      9 5 7
      10 5 7
      11 5 7
      12 6 11




      Edit: as I am dealing with grouped data with dplyr, the ultimate results I wish to get is like below, that is, I am wishing the conditions could be dynamic within each group.



      data <- data.frame(id = c(1,1,2,2,2,3,3,4,4,4,4,4), 
      input = c(1,1,1,1,2,2,3,5,5,5,5,6),
      count=c(0,0,0,0,2,0,1,0,0,0,0,4))

      id input count
      1 1 1 0
      2 1 1 0
      3 2 1 0
      4 2 1 0
      5 2 2 2
      6 3 2 0
      7 3 3 1
      8 4 5 0
      9 4 5 0
      10 4 5 0
      11 4 5 0
      12 4 6 4






      r dplyr






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 19:58









      Henrik

      41.2k992107




      41.2k992107










      asked Nov 23 '18 at 16:07









      RickXManRickXMan

      185




      185
























          3 Answers
          3






          active

          oldest

          votes


















          1














          Here is an option with tidyverse



          library(tidyverse)
          data %>%
          mutate(count = map_int(input, ~ sum(.x > input)))
          # input count
          #1 1 0
          #2 1 0
          #3 1 0
          #4 1 0
          #5 2 4
          #6 2 4
          #7 3 6
          #8 5 7
          #9 5 7
          #10 5 7
          #11 5 7
          #12 6 11


          Update



          With the updated data, add the group by 'id' in the above code



          data %>% 
          group_by(id) %>%
          mutate(count1 = map_int(input, ~ sum(.x > input)))
          # A tibble: 12 x 4
          # Groups: id [4]
          # id input count count1
          # <dbl> <dbl> <dbl> <int>
          # 1 1 1 0 0
          # 2 1 1 0 0
          # 3 2 1 0 0
          # 4 2 1 0 0
          # 5 2 2 2 2
          # 6 3 2 0 0
          # 7 3 3 1 1
          # 8 4 5 0 0
          # 9 4 5 0 0
          #10 4 5 0 0
          #11 4 5 0 0
          #12 4 6 4 4





          share|improve this answer

































            2














            In base R, we can use sapply and for each input count how many values are greater than itself.



            data$count <- sapply(data$input, function(x) sum(x > data$input))

            data

            # input count
            #1 1 0
            #2 1 0
            #3 1 0
            #4 1 0
            #5 2 4
            #6 2 4
            #7 3 6
            #8 5 7
            #9 5 7
            #10 5 7
            #11 5 7
            #12 6 11




            With dplyr one way would be using rowwise function and following the same logic.



            library(dplyr)

            data %>%
            rowwise() %>%
            mutate(count = sum(input > data$input))





            share|improve this answer
























            • Thank you very much for your answer! Since I am dealing with group data using dplyr, and the example dataset I presented as is one of the group in my actual dataset, I am just wondering if there is way using dplyr (e.g. using mutate). Thanks!

              – RickXMan
              Nov 23 '18 at 16:30











            • @RickXMan I have shown a way using mutate in the answer for your example.

              – Ronak Shah
              Nov 23 '18 at 16:33











            • Thanks @Ronak Shah . I tried to add group_by before it, but it seems rowwise is not compatible with group_by?

              – RickXMan
              Nov 23 '18 at 17:05



















            2














            1. outer and rowSums



            data$count <- with(data, rowSums(outer(input, input, `>`)))




            2. table and cumsum



            tt <- cumsum(table(data$input))
            v <- setNames(c(0, head(tt, -1)), c(head(names(tt), -1), tail(names(tt), 1)))
            data$count <- v[match(data$input, names(v))]




            3. data.table non-equi join



            Perhaps more efficient with a non-equi join in data.table. Count number of rows (.N) for each match (by = .EACHI).



            library(data.table)
            setDT(data)
            data[data, on = .(input < input), .N, by = .EACHI]




            If your data is grouped by 'id', as in your update, join on that variable as well:



            data[data, on = .(id, input < input), .N, by = .EACHI]

            # id input N
            # 1: 1 1 0
            # 2: 1 1 0
            # 3: 2 1 0
            # 4: 2 1 0
            # 5: 2 2 2
            # 6: 3 2 0
            # 7: 3 3 1
            # 8: 4 5 0
            # 9: 4 5 0
            # 10: 4 5 0
            # 11: 4 5 0
            # 12: 4 6 4





            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',
              autoActivateHeartbeat: false,
              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%2f53449830%2fcount-number-of-values-which-are-less-than-current-value%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









              1














              Here is an option with tidyverse



              library(tidyverse)
              data %>%
              mutate(count = map_int(input, ~ sum(.x > input)))
              # input count
              #1 1 0
              #2 1 0
              #3 1 0
              #4 1 0
              #5 2 4
              #6 2 4
              #7 3 6
              #8 5 7
              #9 5 7
              #10 5 7
              #11 5 7
              #12 6 11


              Update



              With the updated data, add the group by 'id' in the above code



              data %>% 
              group_by(id) %>%
              mutate(count1 = map_int(input, ~ sum(.x > input)))
              # A tibble: 12 x 4
              # Groups: id [4]
              # id input count count1
              # <dbl> <dbl> <dbl> <int>
              # 1 1 1 0 0
              # 2 1 1 0 0
              # 3 2 1 0 0
              # 4 2 1 0 0
              # 5 2 2 2 2
              # 6 3 2 0 0
              # 7 3 3 1 1
              # 8 4 5 0 0
              # 9 4 5 0 0
              #10 4 5 0 0
              #11 4 5 0 0
              #12 4 6 4 4





              share|improve this answer






























                1














                Here is an option with tidyverse



                library(tidyverse)
                data %>%
                mutate(count = map_int(input, ~ sum(.x > input)))
                # input count
                #1 1 0
                #2 1 0
                #3 1 0
                #4 1 0
                #5 2 4
                #6 2 4
                #7 3 6
                #8 5 7
                #9 5 7
                #10 5 7
                #11 5 7
                #12 6 11


                Update



                With the updated data, add the group by 'id' in the above code



                data %>% 
                group_by(id) %>%
                mutate(count1 = map_int(input, ~ sum(.x > input)))
                # A tibble: 12 x 4
                # Groups: id [4]
                # id input count count1
                # <dbl> <dbl> <dbl> <int>
                # 1 1 1 0 0
                # 2 1 1 0 0
                # 3 2 1 0 0
                # 4 2 1 0 0
                # 5 2 2 2 2
                # 6 3 2 0 0
                # 7 3 3 1 1
                # 8 4 5 0 0
                # 9 4 5 0 0
                #10 4 5 0 0
                #11 4 5 0 0
                #12 4 6 4 4





                share|improve this answer




























                  1












                  1








                  1







                  Here is an option with tidyverse



                  library(tidyverse)
                  data %>%
                  mutate(count = map_int(input, ~ sum(.x > input)))
                  # input count
                  #1 1 0
                  #2 1 0
                  #3 1 0
                  #4 1 0
                  #5 2 4
                  #6 2 4
                  #7 3 6
                  #8 5 7
                  #9 5 7
                  #10 5 7
                  #11 5 7
                  #12 6 11


                  Update



                  With the updated data, add the group by 'id' in the above code



                  data %>% 
                  group_by(id) %>%
                  mutate(count1 = map_int(input, ~ sum(.x > input)))
                  # A tibble: 12 x 4
                  # Groups: id [4]
                  # id input count count1
                  # <dbl> <dbl> <dbl> <int>
                  # 1 1 1 0 0
                  # 2 1 1 0 0
                  # 3 2 1 0 0
                  # 4 2 1 0 0
                  # 5 2 2 2 2
                  # 6 3 2 0 0
                  # 7 3 3 1 1
                  # 8 4 5 0 0
                  # 9 4 5 0 0
                  #10 4 5 0 0
                  #11 4 5 0 0
                  #12 4 6 4 4





                  share|improve this answer















                  Here is an option with tidyverse



                  library(tidyverse)
                  data %>%
                  mutate(count = map_int(input, ~ sum(.x > input)))
                  # input count
                  #1 1 0
                  #2 1 0
                  #3 1 0
                  #4 1 0
                  #5 2 4
                  #6 2 4
                  #7 3 6
                  #8 5 7
                  #9 5 7
                  #10 5 7
                  #11 5 7
                  #12 6 11


                  Update



                  With the updated data, add the group by 'id' in the above code



                  data %>% 
                  group_by(id) %>%
                  mutate(count1 = map_int(input, ~ sum(.x > input)))
                  # A tibble: 12 x 4
                  # Groups: id [4]
                  # id input count count1
                  # <dbl> <dbl> <dbl> <int>
                  # 1 1 1 0 0
                  # 2 1 1 0 0
                  # 3 2 1 0 0
                  # 4 2 1 0 0
                  # 5 2 2 2 2
                  # 6 3 2 0 0
                  # 7 3 3 1 1
                  # 8 4 5 0 0
                  # 9 4 5 0 0
                  #10 4 5 0 0
                  #11 4 5 0 0
                  #12 4 6 4 4






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 17:15

























                  answered Nov 23 '18 at 16:26









                  akrunakrun

                  401k13191265




                  401k13191265

























                      2














                      In base R, we can use sapply and for each input count how many values are greater than itself.



                      data$count <- sapply(data$input, function(x) sum(x > data$input))

                      data

                      # input count
                      #1 1 0
                      #2 1 0
                      #3 1 0
                      #4 1 0
                      #5 2 4
                      #6 2 4
                      #7 3 6
                      #8 5 7
                      #9 5 7
                      #10 5 7
                      #11 5 7
                      #12 6 11




                      With dplyr one way would be using rowwise function and following the same logic.



                      library(dplyr)

                      data %>%
                      rowwise() %>%
                      mutate(count = sum(input > data$input))





                      share|improve this answer
























                      • Thank you very much for your answer! Since I am dealing with group data using dplyr, and the example dataset I presented as is one of the group in my actual dataset, I am just wondering if there is way using dplyr (e.g. using mutate). Thanks!

                        – RickXMan
                        Nov 23 '18 at 16:30











                      • @RickXMan I have shown a way using mutate in the answer for your example.

                        – Ronak Shah
                        Nov 23 '18 at 16:33











                      • Thanks @Ronak Shah . I tried to add group_by before it, but it seems rowwise is not compatible with group_by?

                        – RickXMan
                        Nov 23 '18 at 17:05
















                      2














                      In base R, we can use sapply and for each input count how many values are greater than itself.



                      data$count <- sapply(data$input, function(x) sum(x > data$input))

                      data

                      # input count
                      #1 1 0
                      #2 1 0
                      #3 1 0
                      #4 1 0
                      #5 2 4
                      #6 2 4
                      #7 3 6
                      #8 5 7
                      #9 5 7
                      #10 5 7
                      #11 5 7
                      #12 6 11




                      With dplyr one way would be using rowwise function and following the same logic.



                      library(dplyr)

                      data %>%
                      rowwise() %>%
                      mutate(count = sum(input > data$input))





                      share|improve this answer
























                      • Thank you very much for your answer! Since I am dealing with group data using dplyr, and the example dataset I presented as is one of the group in my actual dataset, I am just wondering if there is way using dplyr (e.g. using mutate). Thanks!

                        – RickXMan
                        Nov 23 '18 at 16:30











                      • @RickXMan I have shown a way using mutate in the answer for your example.

                        – Ronak Shah
                        Nov 23 '18 at 16:33











                      • Thanks @Ronak Shah . I tried to add group_by before it, but it seems rowwise is not compatible with group_by?

                        – RickXMan
                        Nov 23 '18 at 17:05














                      2












                      2








                      2







                      In base R, we can use sapply and for each input count how many values are greater than itself.



                      data$count <- sapply(data$input, function(x) sum(x > data$input))

                      data

                      # input count
                      #1 1 0
                      #2 1 0
                      #3 1 0
                      #4 1 0
                      #5 2 4
                      #6 2 4
                      #7 3 6
                      #8 5 7
                      #9 5 7
                      #10 5 7
                      #11 5 7
                      #12 6 11




                      With dplyr one way would be using rowwise function and following the same logic.



                      library(dplyr)

                      data %>%
                      rowwise() %>%
                      mutate(count = sum(input > data$input))





                      share|improve this answer













                      In base R, we can use sapply and for each input count how many values are greater than itself.



                      data$count <- sapply(data$input, function(x) sum(x > data$input))

                      data

                      # input count
                      #1 1 0
                      #2 1 0
                      #3 1 0
                      #4 1 0
                      #5 2 4
                      #6 2 4
                      #7 3 6
                      #8 5 7
                      #9 5 7
                      #10 5 7
                      #11 5 7
                      #12 6 11




                      With dplyr one way would be using rowwise function and following the same logic.



                      library(dplyr)

                      data %>%
                      rowwise() %>%
                      mutate(count = sum(input > data$input))






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 23 '18 at 16:14









                      Ronak ShahRonak Shah

                      34.9k103856




                      34.9k103856













                      • Thank you very much for your answer! Since I am dealing with group data using dplyr, and the example dataset I presented as is one of the group in my actual dataset, I am just wondering if there is way using dplyr (e.g. using mutate). Thanks!

                        – RickXMan
                        Nov 23 '18 at 16:30











                      • @RickXMan I have shown a way using mutate in the answer for your example.

                        – Ronak Shah
                        Nov 23 '18 at 16:33











                      • Thanks @Ronak Shah . I tried to add group_by before it, but it seems rowwise is not compatible with group_by?

                        – RickXMan
                        Nov 23 '18 at 17:05



















                      • Thank you very much for your answer! Since I am dealing with group data using dplyr, and the example dataset I presented as is one of the group in my actual dataset, I am just wondering if there is way using dplyr (e.g. using mutate). Thanks!

                        – RickXMan
                        Nov 23 '18 at 16:30











                      • @RickXMan I have shown a way using mutate in the answer for your example.

                        – Ronak Shah
                        Nov 23 '18 at 16:33











                      • Thanks @Ronak Shah . I tried to add group_by before it, but it seems rowwise is not compatible with group_by?

                        – RickXMan
                        Nov 23 '18 at 17:05

















                      Thank you very much for your answer! Since I am dealing with group data using dplyr, and the example dataset I presented as is one of the group in my actual dataset, I am just wondering if there is way using dplyr (e.g. using mutate). Thanks!

                      – RickXMan
                      Nov 23 '18 at 16:30





                      Thank you very much for your answer! Since I am dealing with group data using dplyr, and the example dataset I presented as is one of the group in my actual dataset, I am just wondering if there is way using dplyr (e.g. using mutate). Thanks!

                      – RickXMan
                      Nov 23 '18 at 16:30













                      @RickXMan I have shown a way using mutate in the answer for your example.

                      – Ronak Shah
                      Nov 23 '18 at 16:33





                      @RickXMan I have shown a way using mutate in the answer for your example.

                      – Ronak Shah
                      Nov 23 '18 at 16:33













                      Thanks @Ronak Shah . I tried to add group_by before it, but it seems rowwise is not compatible with group_by?

                      – RickXMan
                      Nov 23 '18 at 17:05





                      Thanks @Ronak Shah . I tried to add group_by before it, but it seems rowwise is not compatible with group_by?

                      – RickXMan
                      Nov 23 '18 at 17:05











                      2














                      1. outer and rowSums



                      data$count <- with(data, rowSums(outer(input, input, `>`)))




                      2. table and cumsum



                      tt <- cumsum(table(data$input))
                      v <- setNames(c(0, head(tt, -1)), c(head(names(tt), -1), tail(names(tt), 1)))
                      data$count <- v[match(data$input, names(v))]




                      3. data.table non-equi join



                      Perhaps more efficient with a non-equi join in data.table. Count number of rows (.N) for each match (by = .EACHI).



                      library(data.table)
                      setDT(data)
                      data[data, on = .(input < input), .N, by = .EACHI]




                      If your data is grouped by 'id', as in your update, join on that variable as well:



                      data[data, on = .(id, input < input), .N, by = .EACHI]

                      # id input N
                      # 1: 1 1 0
                      # 2: 1 1 0
                      # 3: 2 1 0
                      # 4: 2 1 0
                      # 5: 2 2 2
                      # 6: 3 2 0
                      # 7: 3 3 1
                      # 8: 4 5 0
                      # 9: 4 5 0
                      # 10: 4 5 0
                      # 11: 4 5 0
                      # 12: 4 6 4





                      share|improve this answer






























                        2














                        1. outer and rowSums



                        data$count <- with(data, rowSums(outer(input, input, `>`)))




                        2. table and cumsum



                        tt <- cumsum(table(data$input))
                        v <- setNames(c(0, head(tt, -1)), c(head(names(tt), -1), tail(names(tt), 1)))
                        data$count <- v[match(data$input, names(v))]




                        3. data.table non-equi join



                        Perhaps more efficient with a non-equi join in data.table. Count number of rows (.N) for each match (by = .EACHI).



                        library(data.table)
                        setDT(data)
                        data[data, on = .(input < input), .N, by = .EACHI]




                        If your data is grouped by 'id', as in your update, join on that variable as well:



                        data[data, on = .(id, input < input), .N, by = .EACHI]

                        # id input N
                        # 1: 1 1 0
                        # 2: 1 1 0
                        # 3: 2 1 0
                        # 4: 2 1 0
                        # 5: 2 2 2
                        # 6: 3 2 0
                        # 7: 3 3 1
                        # 8: 4 5 0
                        # 9: 4 5 0
                        # 10: 4 5 0
                        # 11: 4 5 0
                        # 12: 4 6 4





                        share|improve this answer




























                          2












                          2








                          2







                          1. outer and rowSums



                          data$count <- with(data, rowSums(outer(input, input, `>`)))




                          2. table and cumsum



                          tt <- cumsum(table(data$input))
                          v <- setNames(c(0, head(tt, -1)), c(head(names(tt), -1), tail(names(tt), 1)))
                          data$count <- v[match(data$input, names(v))]




                          3. data.table non-equi join



                          Perhaps more efficient with a non-equi join in data.table. Count number of rows (.N) for each match (by = .EACHI).



                          library(data.table)
                          setDT(data)
                          data[data, on = .(input < input), .N, by = .EACHI]




                          If your data is grouped by 'id', as in your update, join on that variable as well:



                          data[data, on = .(id, input < input), .N, by = .EACHI]

                          # id input N
                          # 1: 1 1 0
                          # 2: 1 1 0
                          # 3: 2 1 0
                          # 4: 2 1 0
                          # 5: 2 2 2
                          # 6: 3 2 0
                          # 7: 3 3 1
                          # 8: 4 5 0
                          # 9: 4 5 0
                          # 10: 4 5 0
                          # 11: 4 5 0
                          # 12: 4 6 4





                          share|improve this answer















                          1. outer and rowSums



                          data$count <- with(data, rowSums(outer(input, input, `>`)))




                          2. table and cumsum



                          tt <- cumsum(table(data$input))
                          v <- setNames(c(0, head(tt, -1)), c(head(names(tt), -1), tail(names(tt), 1)))
                          data$count <- v[match(data$input, names(v))]




                          3. data.table non-equi join



                          Perhaps more efficient with a non-equi join in data.table. Count number of rows (.N) for each match (by = .EACHI).



                          library(data.table)
                          setDT(data)
                          data[data, on = .(input < input), .N, by = .EACHI]




                          If your data is grouped by 'id', as in your update, join on that variable as well:



                          data[data, on = .(id, input < input), .N, by = .EACHI]

                          # id input N
                          # 1: 1 1 0
                          # 2: 1 1 0
                          # 3: 2 1 0
                          # 4: 2 1 0
                          # 5: 2 2 2
                          # 6: 3 2 0
                          # 7: 3 3 1
                          # 8: 4 5 0
                          # 9: 4 5 0
                          # 10: 4 5 0
                          # 11: 4 5 0
                          # 12: 4 6 4






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 23 '18 at 19:57

























                          answered Nov 23 '18 at 16:28









                          HenrikHenrik

                          41.2k992107




                          41.2k992107






























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449830%2fcount-number-of-values-which-are-less-than-current-value%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

                              Berounka

                              Sphinx de Gizeh

                              Different font size/position of beamer's navigation symbols template's content depending on regular/plain...