How to break one column to multiple rows with each cell row limit









up vote
0
down vote

favorite












Thank you for your time.



I am unable to figure out a way to perform the following operation.
The logic for the operation is to break down a column to multiple rows with limits on each cell value.
for 1st row and second row max is 250. subsequent are 500 max. each value in amount should be broken down to multiple rows based on these set of rules.
for ex:- ID 1 has 1500 as amount. when being broken down. the first row and second row can allow max 250 as a values. and next rows accommodate the rest until the amount value is maxed out.
Data is shown below



Raw data



ID Name Amount
1 aa 1500
2 bb 2000
3 cc 1000
4 dd 500


Final output



ID Name Amount
1 aa 250
1 aa 250
1 aa 500
1 aa 500
2 bb 250
2 bb 250
2 bb 500
2 bb 500
2 bb 500
2 bb 500
3 cc 250
3 cc 250
3 cc 500
4 dd 250
4 dd 250









share|improve this question



























    up vote
    0
    down vote

    favorite












    Thank you for your time.



    I am unable to figure out a way to perform the following operation.
    The logic for the operation is to break down a column to multiple rows with limits on each cell value.
    for 1st row and second row max is 250. subsequent are 500 max. each value in amount should be broken down to multiple rows based on these set of rules.
    for ex:- ID 1 has 1500 as amount. when being broken down. the first row and second row can allow max 250 as a values. and next rows accommodate the rest until the amount value is maxed out.
    Data is shown below



    Raw data



    ID Name Amount
    1 aa 1500
    2 bb 2000
    3 cc 1000
    4 dd 500


    Final output



    ID Name Amount
    1 aa 250
    1 aa 250
    1 aa 500
    1 aa 500
    2 bb 250
    2 bb 250
    2 bb 500
    2 bb 500
    2 bb 500
    2 bb 500
    3 cc 250
    3 cc 250
    3 cc 500
    4 dd 250
    4 dd 250









    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Thank you for your time.



      I am unable to figure out a way to perform the following operation.
      The logic for the operation is to break down a column to multiple rows with limits on each cell value.
      for 1st row and second row max is 250. subsequent are 500 max. each value in amount should be broken down to multiple rows based on these set of rules.
      for ex:- ID 1 has 1500 as amount. when being broken down. the first row and second row can allow max 250 as a values. and next rows accommodate the rest until the amount value is maxed out.
      Data is shown below



      Raw data



      ID Name Amount
      1 aa 1500
      2 bb 2000
      3 cc 1000
      4 dd 500


      Final output



      ID Name Amount
      1 aa 250
      1 aa 250
      1 aa 500
      1 aa 500
      2 bb 250
      2 bb 250
      2 bb 500
      2 bb 500
      2 bb 500
      2 bb 500
      3 cc 250
      3 cc 250
      3 cc 500
      4 dd 250
      4 dd 250









      share|improve this question















      Thank you for your time.



      I am unable to figure out a way to perform the following operation.
      The logic for the operation is to break down a column to multiple rows with limits on each cell value.
      for 1st row and second row max is 250. subsequent are 500 max. each value in amount should be broken down to multiple rows based on these set of rules.
      for ex:- ID 1 has 1500 as amount. when being broken down. the first row and second row can allow max 250 as a values. and next rows accommodate the rest until the amount value is maxed out.
      Data is shown below



      Raw data



      ID Name Amount
      1 aa 1500
      2 bb 2000
      3 cc 1000
      4 dd 500


      Final output



      ID Name Amount
      1 aa 250
      1 aa 250
      1 aa 500
      1 aa 500
      2 bb 250
      2 bb 250
      2 bb 500
      2 bb 500
      2 bb 500
      2 bb 500
      3 cc 250
      3 cc 250
      3 cc 500
      4 dd 250
      4 dd 250






      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 20:56









      hrbrmstr

      58.3k584143




      58.3k584143










      asked Nov 9 at 20:46









      ShasankC

      11




      11






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Here's how you could do it with loops. Won't be too bad if your data isn't too large.



          x = data.frame("ID"=1:5, "Name"=c("aa","bb","cc","dd","ee"),"Amount"=c(1500,2000,1000,500,800))

          do_that = function(x)
          y = x[0,]
          r = 0
          for (i in 1:NROW(x))
          count = 0
          while (x[i,3] > 0)
          r = r + 1
          y[r,1] = x[i,1]
          y[r,2] = x[i,2]
          y[r,3] = 0
          count = count + 1
          if (count <= 2)
          d = min(x[i,3], 250)
          else
          d = min(x[i,3], 500)

          y[r,3] = y[r,3] + d
          x[i,3] = x[i,3] - d


          return (y)


          y = do_that(x)
          y


          I even added a bonus row that's not a multiple of 250 or 500.






          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%2f53233037%2fhow-to-break-one-column-to-multiple-rows-with-each-cell-row-limit%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Here's how you could do it with loops. Won't be too bad if your data isn't too large.



            x = data.frame("ID"=1:5, "Name"=c("aa","bb","cc","dd","ee"),"Amount"=c(1500,2000,1000,500,800))

            do_that = function(x)
            y = x[0,]
            r = 0
            for (i in 1:NROW(x))
            count = 0
            while (x[i,3] > 0)
            r = r + 1
            y[r,1] = x[i,1]
            y[r,2] = x[i,2]
            y[r,3] = 0
            count = count + 1
            if (count <= 2)
            d = min(x[i,3], 250)
            else
            d = min(x[i,3], 500)

            y[r,3] = y[r,3] + d
            x[i,3] = x[i,3] - d


            return (y)


            y = do_that(x)
            y


            I even added a bonus row that's not a multiple of 250 or 500.






            share|improve this answer
























              up vote
              0
              down vote













              Here's how you could do it with loops. Won't be too bad if your data isn't too large.



              x = data.frame("ID"=1:5, "Name"=c("aa","bb","cc","dd","ee"),"Amount"=c(1500,2000,1000,500,800))

              do_that = function(x)
              y = x[0,]
              r = 0
              for (i in 1:NROW(x))
              count = 0
              while (x[i,3] > 0)
              r = r + 1
              y[r,1] = x[i,1]
              y[r,2] = x[i,2]
              y[r,3] = 0
              count = count + 1
              if (count <= 2)
              d = min(x[i,3], 250)
              else
              d = min(x[i,3], 500)

              y[r,3] = y[r,3] + d
              x[i,3] = x[i,3] - d


              return (y)


              y = do_that(x)
              y


              I even added a bonus row that's not a multiple of 250 or 500.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                Here's how you could do it with loops. Won't be too bad if your data isn't too large.



                x = data.frame("ID"=1:5, "Name"=c("aa","bb","cc","dd","ee"),"Amount"=c(1500,2000,1000,500,800))

                do_that = function(x)
                y = x[0,]
                r = 0
                for (i in 1:NROW(x))
                count = 0
                while (x[i,3] > 0)
                r = r + 1
                y[r,1] = x[i,1]
                y[r,2] = x[i,2]
                y[r,3] = 0
                count = count + 1
                if (count <= 2)
                d = min(x[i,3], 250)
                else
                d = min(x[i,3], 500)

                y[r,3] = y[r,3] + d
                x[i,3] = x[i,3] - d


                return (y)


                y = do_that(x)
                y


                I even added a bonus row that's not a multiple of 250 or 500.






                share|improve this answer












                Here's how you could do it with loops. Won't be too bad if your data isn't too large.



                x = data.frame("ID"=1:5, "Name"=c("aa","bb","cc","dd","ee"),"Amount"=c(1500,2000,1000,500,800))

                do_that = function(x)
                y = x[0,]
                r = 0
                for (i in 1:NROW(x))
                count = 0
                while (x[i,3] > 0)
                r = r + 1
                y[r,1] = x[i,1]
                y[r,2] = x[i,2]
                y[r,3] = 0
                count = count + 1
                if (count <= 2)
                d = min(x[i,3], 250)
                else
                d = min(x[i,3], 500)

                y[r,3] = y[r,3] + d
                x[i,3] = x[i,3] - d


                return (y)


                y = do_that(x)
                y


                I even added a bonus row that's not a multiple of 250 or 500.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 9 at 21:09









                mickey

                58114




                58114



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233037%2fhow-to-break-one-column-to-multiple-rows-with-each-cell-row-limit%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

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo