How to accumulate sum by row per column in R
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
add a comment |
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
If your problem got solved please choose an answer.
– Andre Elrico
Nov 22 '18 at 11:12
add a comment |
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
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
r dataframe sum
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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)
add a comment |
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.
}
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 19 '18 at 11:37
duckmayrduckmayr
7,54811326
7,54811326
add a comment |
add a comment |
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.
}
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
add a comment |
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.
}
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
add a comment |
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.
}
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.
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
If your problem got solved please choose an answer.
– Andre Elrico
Nov 22 '18 at 11:12