How to accumulate sum by row per column in R












0















I would like to do a rowsum (acumulate) per column in the same row. What I spect is something lik this:



test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
colnames(test) = c('Position','Position1','Position2','Position3','Position4')
> test
Position Position1 Position2 Position3 Position4
1 0 0 0 0 0
2 1 0 0 0 0
3 1 1 0 0 0
4 1 1 1 0 0
5 0 1 1 1 0
6 0 0 1 1 1

result = data.frame(c(0,1,2,3,0,0),c(0,0,1,2,3,0),c(0,0,0,1,2,3),c(0,0,0,0,1,2),c(0,0,0,0,0,1))
colnames(result) = c('Position','Position1','Position2','Position3','Position4')
> result
Position Position1 Position2 Position3 Position4
1 0 0 0 0 0
2 1 0 0 0 0
3 2 1 0 0 0
4 3 2 1 0 0
5 0 3 2 1 0
6 0 0 3 2 1


The data.frame result is what I want.



Thanks










share|improve this question

























  • If your problem got solved please choose an answer.

    – Andre Elrico
    Nov 22 '18 at 11:12
















0















I would like to do a rowsum (acumulate) per column in the same row. What I spect is something lik this:



test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
colnames(test) = c('Position','Position1','Position2','Position3','Position4')
> test
Position Position1 Position2 Position3 Position4
1 0 0 0 0 0
2 1 0 0 0 0
3 1 1 0 0 0
4 1 1 1 0 0
5 0 1 1 1 0
6 0 0 1 1 1

result = data.frame(c(0,1,2,3,0,0),c(0,0,1,2,3,0),c(0,0,0,1,2,3),c(0,0,0,0,1,2),c(0,0,0,0,0,1))
colnames(result) = c('Position','Position1','Position2','Position3','Position4')
> result
Position Position1 Position2 Position3 Position4
1 0 0 0 0 0
2 1 0 0 0 0
3 2 1 0 0 0
4 3 2 1 0 0
5 0 3 2 1 0
6 0 0 3 2 1


The data.frame result is what I want.



Thanks










share|improve this question

























  • If your problem got solved please choose an answer.

    – Andre Elrico
    Nov 22 '18 at 11:12














0












0








0








I would like to do a rowsum (acumulate) per column in the same row. What I spect is something lik this:



test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
colnames(test) = c('Position','Position1','Position2','Position3','Position4')
> test
Position Position1 Position2 Position3 Position4
1 0 0 0 0 0
2 1 0 0 0 0
3 1 1 0 0 0
4 1 1 1 0 0
5 0 1 1 1 0
6 0 0 1 1 1

result = data.frame(c(0,1,2,3,0,0),c(0,0,1,2,3,0),c(0,0,0,1,2,3),c(0,0,0,0,1,2),c(0,0,0,0,0,1))
colnames(result) = c('Position','Position1','Position2','Position3','Position4')
> result
Position Position1 Position2 Position3 Position4
1 0 0 0 0 0
2 1 0 0 0 0
3 2 1 0 0 0
4 3 2 1 0 0
5 0 3 2 1 0
6 0 0 3 2 1


The data.frame result is what I want.



Thanks










share|improve this question
















I would like to do a rowsum (acumulate) per column in the same row. What I spect is something lik this:



test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
colnames(test) = c('Position','Position1','Position2','Position3','Position4')
> test
Position Position1 Position2 Position3 Position4
1 0 0 0 0 0
2 1 0 0 0 0
3 1 1 0 0 0
4 1 1 1 0 0
5 0 1 1 1 0
6 0 0 1 1 1

result = data.frame(c(0,1,2,3,0,0),c(0,0,1,2,3,0),c(0,0,0,1,2,3),c(0,0,0,0,1,2),c(0,0,0,0,0,1))
colnames(result) = c('Position','Position1','Position2','Position3','Position4')
> result
Position Position1 Position2 Position3 Position4
1 0 0 0 0 0
2 1 0 0 0 0
3 2 1 0 0 0
4 3 2 1 0 0
5 0 3 2 1 0
6 0 0 3 2 1


The data.frame result is what I want.



Thanks







r dataframe sum






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 11:46









jogo

10k92135




10k92135










asked Nov 19 '18 at 11:33









Alberto AguileraAlberto Aguilera

498




498













  • If your problem got solved please choose an answer.

    – Andre Elrico
    Nov 22 '18 at 11:12



















  • If your problem got solved please choose an answer.

    – Andre Elrico
    Nov 22 '18 at 11:12

















If your problem got solved please choose an answer.

– Andre Elrico
Nov 22 '18 at 11:12





If your problem got solved please choose an answer.

– Andre Elrico
Nov 22 '18 at 11:12












2 Answers
2






active

oldest

votes


















2














At first I thought you just needed to apply cumsum() to each column, but it appears you also need to multiply the cumulative sum by the columns themselves to ensure the zeros are preserved:





test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
colnames(test) = c('Position','Position1','Position2','Position3','Position4')
apply(test, 2, function(x) x * cumsum(x))
#> Position Position1 Position2 Position3 Position4
#> [1,] 0 0 0 0 0
#> [2,] 1 0 0 0 0
#> [3,] 2 1 0 0 0
#> [4,] 3 2 1 0 0
#> [5,] 0 3 2 1 0
#> [6,] 0 0 3 2 1


Created on 2018-11-19 by the reprex package (v0.2.1)



If you truly just want a cumulative sum for each column, you can just use apply(df, 2, cumsum):





test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
colnames(test) = c('Position','Position1','Position2','Position3','Position4')
apply(test, 2, cumsum)
#> Position Position1 Position2 Position3 Position4
#> [1,] 0 0 0 0 0
#> [2,] 1 0 0 0 0
#> [3,] 2 1 0 0 0
#> [4,] 3 2 1 0 0
#> [5,] 3 3 2 1 0
#> [6,] 3 3 3 2 1


Created on 2018-11-19 by the reprex package (v0.2.1)






share|improve this answer































    1














    As a similar alternative to @duckmayr



    apply(test, 2, function(x) {x[x] <- 1:sum(x); x})

    # Position Position1 Position2 Position3 Position4
    #[1,] 0 0 0 0 0
    #[2,] 1 0 0 0 0
    #[3,] 2 1 0 0 0
    #[4,] 3 2 1 0 0
    #[5,] 0 3 2 1 0
    #[6,] 0 0 3 2 1




    further detail:



    {        
    x[x] <- # this is a special case because you want to replace the ones. 1 = TRUE in R internally. So you assign values where x == 1. More general would be to write x[x == 1]
    1:sum(x) # 1:N makes a sequence from 1, 2, 3, ..., N. sum(x) will return the amount of ones in a col. Again, more general would be `sum(x == 1)`.
    ; x # This part is similar to return(x), We want to return the changes we made in x.
    }





    share|improve this answer


























    • Could you please explain the solution?

      – Alberto Aguilera
      Nov 19 '18 at 13:01











    • sure,what part you don't understand?

      – Andre Elrico
      Nov 19 '18 at 13:23













    • {x[x == 1] <- 1:sum(x); x}) this one. Thanks

      – Alberto Aguilera
      Nov 19 '18 at 19:42











    • @AlbertoAguilera I've added a few explanations.

      – Andre Elrico
      Nov 20 '18 at 8:33











    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%2f53373768%2fhow-to-accumulate-sum-by-row-per-column-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









    2














    At first I thought you just needed to apply cumsum() to each column, but it appears you also need to multiply the cumulative sum by the columns themselves to ensure the zeros are preserved:





    test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
    colnames(test) = c('Position','Position1','Position2','Position3','Position4')
    apply(test, 2, function(x) x * cumsum(x))
    #> Position Position1 Position2 Position3 Position4
    #> [1,] 0 0 0 0 0
    #> [2,] 1 0 0 0 0
    #> [3,] 2 1 0 0 0
    #> [4,] 3 2 1 0 0
    #> [5,] 0 3 2 1 0
    #> [6,] 0 0 3 2 1


    Created on 2018-11-19 by the reprex package (v0.2.1)



    If you truly just want a cumulative sum for each column, you can just use apply(df, 2, cumsum):





    test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
    colnames(test) = c('Position','Position1','Position2','Position3','Position4')
    apply(test, 2, cumsum)
    #> Position Position1 Position2 Position3 Position4
    #> [1,] 0 0 0 0 0
    #> [2,] 1 0 0 0 0
    #> [3,] 2 1 0 0 0
    #> [4,] 3 2 1 0 0
    #> [5,] 3 3 2 1 0
    #> [6,] 3 3 3 2 1


    Created on 2018-11-19 by the reprex package (v0.2.1)






    share|improve this answer




























      2














      At first I thought you just needed to apply cumsum() to each column, but it appears you also need to multiply the cumulative sum by the columns themselves to ensure the zeros are preserved:





      test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
      colnames(test) = c('Position','Position1','Position2','Position3','Position4')
      apply(test, 2, function(x) x * cumsum(x))
      #> Position Position1 Position2 Position3 Position4
      #> [1,] 0 0 0 0 0
      #> [2,] 1 0 0 0 0
      #> [3,] 2 1 0 0 0
      #> [4,] 3 2 1 0 0
      #> [5,] 0 3 2 1 0
      #> [6,] 0 0 3 2 1


      Created on 2018-11-19 by the reprex package (v0.2.1)



      If you truly just want a cumulative sum for each column, you can just use apply(df, 2, cumsum):





      test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
      colnames(test) = c('Position','Position1','Position2','Position3','Position4')
      apply(test, 2, cumsum)
      #> Position Position1 Position2 Position3 Position4
      #> [1,] 0 0 0 0 0
      #> [2,] 1 0 0 0 0
      #> [3,] 2 1 0 0 0
      #> [4,] 3 2 1 0 0
      #> [5,] 3 3 2 1 0
      #> [6,] 3 3 3 2 1


      Created on 2018-11-19 by the reprex package (v0.2.1)






      share|improve this answer


























        2












        2








        2







        At first I thought you just needed to apply cumsum() to each column, but it appears you also need to multiply the cumulative sum by the columns themselves to ensure the zeros are preserved:





        test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
        colnames(test) = c('Position','Position1','Position2','Position3','Position4')
        apply(test, 2, function(x) x * cumsum(x))
        #> Position Position1 Position2 Position3 Position4
        #> [1,] 0 0 0 0 0
        #> [2,] 1 0 0 0 0
        #> [3,] 2 1 0 0 0
        #> [4,] 3 2 1 0 0
        #> [5,] 0 3 2 1 0
        #> [6,] 0 0 3 2 1


        Created on 2018-11-19 by the reprex package (v0.2.1)



        If you truly just want a cumulative sum for each column, you can just use apply(df, 2, cumsum):





        test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
        colnames(test) = c('Position','Position1','Position2','Position3','Position4')
        apply(test, 2, cumsum)
        #> Position Position1 Position2 Position3 Position4
        #> [1,] 0 0 0 0 0
        #> [2,] 1 0 0 0 0
        #> [3,] 2 1 0 0 0
        #> [4,] 3 2 1 0 0
        #> [5,] 3 3 2 1 0
        #> [6,] 3 3 3 2 1


        Created on 2018-11-19 by the reprex package (v0.2.1)






        share|improve this answer













        At first I thought you just needed to apply cumsum() to each column, but it appears you also need to multiply the cumulative sum by the columns themselves to ensure the zeros are preserved:





        test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
        colnames(test) = c('Position','Position1','Position2','Position3','Position4')
        apply(test, 2, function(x) x * cumsum(x))
        #> Position Position1 Position2 Position3 Position4
        #> [1,] 0 0 0 0 0
        #> [2,] 1 0 0 0 0
        #> [3,] 2 1 0 0 0
        #> [4,] 3 2 1 0 0
        #> [5,] 0 3 2 1 0
        #> [6,] 0 0 3 2 1


        Created on 2018-11-19 by the reprex package (v0.2.1)



        If you truly just want a cumulative sum for each column, you can just use apply(df, 2, cumsum):





        test = data.frame(c(0,1,1,1,0,0),c(0,0,1,1,1,0),c(0,0,0,1,1,1),c(0,0,0,0,1,1),c(0,0,0,0,0,1))
        colnames(test) = c('Position','Position1','Position2','Position3','Position4')
        apply(test, 2, cumsum)
        #> Position Position1 Position2 Position3 Position4
        #> [1,] 0 0 0 0 0
        #> [2,] 1 0 0 0 0
        #> [3,] 2 1 0 0 0
        #> [4,] 3 2 1 0 0
        #> [5,] 3 3 2 1 0
        #> [6,] 3 3 3 2 1


        Created on 2018-11-19 by the reprex package (v0.2.1)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 11:37









        duckmayrduckmayr

        7,54811326




        7,54811326

























            1














            As a similar alternative to @duckmayr



            apply(test, 2, function(x) {x[x] <- 1:sum(x); x})

            # Position Position1 Position2 Position3 Position4
            #[1,] 0 0 0 0 0
            #[2,] 1 0 0 0 0
            #[3,] 2 1 0 0 0
            #[4,] 3 2 1 0 0
            #[5,] 0 3 2 1 0
            #[6,] 0 0 3 2 1




            further detail:



            {        
            x[x] <- # this is a special case because you want to replace the ones. 1 = TRUE in R internally. So you assign values where x == 1. More general would be to write x[x == 1]
            1:sum(x) # 1:N makes a sequence from 1, 2, 3, ..., N. sum(x) will return the amount of ones in a col. Again, more general would be `sum(x == 1)`.
            ; x # This part is similar to return(x), We want to return the changes we made in x.
            }





            share|improve this answer


























            • Could you please explain the solution?

              – Alberto Aguilera
              Nov 19 '18 at 13:01











            • sure,what part you don't understand?

              – Andre Elrico
              Nov 19 '18 at 13:23













            • {x[x == 1] <- 1:sum(x); x}) this one. Thanks

              – Alberto Aguilera
              Nov 19 '18 at 19:42











            • @AlbertoAguilera I've added a few explanations.

              – Andre Elrico
              Nov 20 '18 at 8:33
















            1














            As a similar alternative to @duckmayr



            apply(test, 2, function(x) {x[x] <- 1:sum(x); x})

            # Position Position1 Position2 Position3 Position4
            #[1,] 0 0 0 0 0
            #[2,] 1 0 0 0 0
            #[3,] 2 1 0 0 0
            #[4,] 3 2 1 0 0
            #[5,] 0 3 2 1 0
            #[6,] 0 0 3 2 1




            further detail:



            {        
            x[x] <- # this is a special case because you want to replace the ones. 1 = TRUE in R internally. So you assign values where x == 1. More general would be to write x[x == 1]
            1:sum(x) # 1:N makes a sequence from 1, 2, 3, ..., N. sum(x) will return the amount of ones in a col. Again, more general would be `sum(x == 1)`.
            ; x # This part is similar to return(x), We want to return the changes we made in x.
            }





            share|improve this answer


























            • Could you please explain the solution?

              – Alberto Aguilera
              Nov 19 '18 at 13:01











            • sure,what part you don't understand?

              – Andre Elrico
              Nov 19 '18 at 13:23













            • {x[x == 1] <- 1:sum(x); x}) this one. Thanks

              – Alberto Aguilera
              Nov 19 '18 at 19:42











            • @AlbertoAguilera I've added a few explanations.

              – Andre Elrico
              Nov 20 '18 at 8:33














            1












            1








            1







            As a similar alternative to @duckmayr



            apply(test, 2, function(x) {x[x] <- 1:sum(x); x})

            # Position Position1 Position2 Position3 Position4
            #[1,] 0 0 0 0 0
            #[2,] 1 0 0 0 0
            #[3,] 2 1 0 0 0
            #[4,] 3 2 1 0 0
            #[5,] 0 3 2 1 0
            #[6,] 0 0 3 2 1




            further detail:



            {        
            x[x] <- # this is a special case because you want to replace the ones. 1 = TRUE in R internally. So you assign values where x == 1. More general would be to write x[x == 1]
            1:sum(x) # 1:N makes a sequence from 1, 2, 3, ..., N. sum(x) will return the amount of ones in a col. Again, more general would be `sum(x == 1)`.
            ; x # This part is similar to return(x), We want to return the changes we made in x.
            }





            share|improve this answer















            As a similar alternative to @duckmayr



            apply(test, 2, function(x) {x[x] <- 1:sum(x); x})

            # Position Position1 Position2 Position3 Position4
            #[1,] 0 0 0 0 0
            #[2,] 1 0 0 0 0
            #[3,] 2 1 0 0 0
            #[4,] 3 2 1 0 0
            #[5,] 0 3 2 1 0
            #[6,] 0 0 3 2 1




            further detail:



            {        
            x[x] <- # this is a special case because you want to replace the ones. 1 = TRUE in R internally. So you assign values where x == 1. More general would be to write x[x == 1]
            1:sum(x) # 1:N makes a sequence from 1, 2, 3, ..., N. sum(x) will return the amount of ones in a col. Again, more general would be `sum(x == 1)`.
            ; x # This part is similar to return(x), We want to return the changes we made in x.
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 8:32

























            answered Nov 19 '18 at 12:10









            Andre ElricoAndre Elrico

            5,70011029




            5,70011029













            • Could you please explain the solution?

              – Alberto Aguilera
              Nov 19 '18 at 13:01











            • sure,what part you don't understand?

              – Andre Elrico
              Nov 19 '18 at 13:23













            • {x[x == 1] <- 1:sum(x); x}) this one. Thanks

              – Alberto Aguilera
              Nov 19 '18 at 19:42











            • @AlbertoAguilera I've added a few explanations.

              – Andre Elrico
              Nov 20 '18 at 8:33



















            • Could you please explain the solution?

              – Alberto Aguilera
              Nov 19 '18 at 13:01











            • sure,what part you don't understand?

              – Andre Elrico
              Nov 19 '18 at 13:23













            • {x[x == 1] <- 1:sum(x); x}) this one. Thanks

              – Alberto Aguilera
              Nov 19 '18 at 19:42











            • @AlbertoAguilera I've added a few explanations.

              – Andre Elrico
              Nov 20 '18 at 8:33

















            Could you please explain the solution?

            – Alberto Aguilera
            Nov 19 '18 at 13:01





            Could you please explain the solution?

            – Alberto Aguilera
            Nov 19 '18 at 13:01













            sure,what part you don't understand?

            – Andre Elrico
            Nov 19 '18 at 13:23







            sure,what part you don't understand?

            – Andre Elrico
            Nov 19 '18 at 13:23















            {x[x == 1] <- 1:sum(x); x}) this one. Thanks

            – Alberto Aguilera
            Nov 19 '18 at 19:42





            {x[x == 1] <- 1:sum(x); x}) this one. Thanks

            – Alberto Aguilera
            Nov 19 '18 at 19:42













            @AlbertoAguilera I've added a few explanations.

            – Andre Elrico
            Nov 20 '18 at 8:33





            @AlbertoAguilera I've added a few explanations.

            – Andre Elrico
            Nov 20 '18 at 8:33


















            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%2f53373768%2fhow-to-accumulate-sum-by-row-per-column-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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini