How to add sum rows of a table or matrix in R?
I have a matrix in R, that looks like the following:
[,1] [,2] [,3] [,4]
[1,] 0 1 1 1
[2,] 2 12 22 32
[3,] 3 13 23 33
[4,] 4 14 24 34
[5,] 5 15 25 35
[6,] 6 16 26 36
[7,] 7 17 27 37
[8,] 8 18 28 38
[9,] 9 19 29 39
[10,] 10 20 30 40
I want to add every two rows are together, so that [1,] and [2,], create a new vector (say A). I need to do this repeatedly (so then [3,] and [4,] etc). I need to do this in a loop or some other way if that is possible (my actual data has 49 columns and 1000 rows). If possible, I would like it to create a new matrix with all the new data in, so from my original data I would have 500 rows, or here 5 rows?
I have tried the following code:
dips = matrix()
X <- seq(1, by = 2, len = 1000)
for(i in X)
dips[i] = population[i,] + population[i+1,]
I keep getting a warning, "number of items to replace is not a multiple of replacement length". And the data produced doesn't contain all 49 columns.
Sorry if this is a basic question or has already been solved, I am a beginner. Thank you in advance!
r loops matrix add rows
add a comment |
I have a matrix in R, that looks like the following:
[,1] [,2] [,3] [,4]
[1,] 0 1 1 1
[2,] 2 12 22 32
[3,] 3 13 23 33
[4,] 4 14 24 34
[5,] 5 15 25 35
[6,] 6 16 26 36
[7,] 7 17 27 37
[8,] 8 18 28 38
[9,] 9 19 29 39
[10,] 10 20 30 40
I want to add every two rows are together, so that [1,] and [2,], create a new vector (say A). I need to do this repeatedly (so then [3,] and [4,] etc). I need to do this in a loop or some other way if that is possible (my actual data has 49 columns and 1000 rows). If possible, I would like it to create a new matrix with all the new data in, so from my original data I would have 500 rows, or here 5 rows?
I have tried the following code:
dips = matrix()
X <- seq(1, by = 2, len = 1000)
for(i in X)
dips[i] = population[i,] + population[i+1,]
I keep getting a warning, "number of items to replace is not a multiple of replacement length". And the data produced doesn't contain all 49 columns.
Sorry if this is a basic question or has already been solved, I am a beginner. Thank you in advance!
r loops matrix add rows
1
Possible duplicate of To get the sum for every 2 rows in R
– ANG
Nov 12 '18 at 17:21
add a comment |
I have a matrix in R, that looks like the following:
[,1] [,2] [,3] [,4]
[1,] 0 1 1 1
[2,] 2 12 22 32
[3,] 3 13 23 33
[4,] 4 14 24 34
[5,] 5 15 25 35
[6,] 6 16 26 36
[7,] 7 17 27 37
[8,] 8 18 28 38
[9,] 9 19 29 39
[10,] 10 20 30 40
I want to add every two rows are together, so that [1,] and [2,], create a new vector (say A). I need to do this repeatedly (so then [3,] and [4,] etc). I need to do this in a loop or some other way if that is possible (my actual data has 49 columns and 1000 rows). If possible, I would like it to create a new matrix with all the new data in, so from my original data I would have 500 rows, or here 5 rows?
I have tried the following code:
dips = matrix()
X <- seq(1, by = 2, len = 1000)
for(i in X)
dips[i] = population[i,] + population[i+1,]
I keep getting a warning, "number of items to replace is not a multiple of replacement length". And the data produced doesn't contain all 49 columns.
Sorry if this is a basic question or has already been solved, I am a beginner. Thank you in advance!
r loops matrix add rows
I have a matrix in R, that looks like the following:
[,1] [,2] [,3] [,4]
[1,] 0 1 1 1
[2,] 2 12 22 32
[3,] 3 13 23 33
[4,] 4 14 24 34
[5,] 5 15 25 35
[6,] 6 16 26 36
[7,] 7 17 27 37
[8,] 8 18 28 38
[9,] 9 19 29 39
[10,] 10 20 30 40
I want to add every two rows are together, so that [1,] and [2,], create a new vector (say A). I need to do this repeatedly (so then [3,] and [4,] etc). I need to do this in a loop or some other way if that is possible (my actual data has 49 columns and 1000 rows). If possible, I would like it to create a new matrix with all the new data in, so from my original data I would have 500 rows, or here 5 rows?
I have tried the following code:
dips = matrix()
X <- seq(1, by = 2, len = 1000)
for(i in X)
dips[i] = population[i,] + population[i+1,]
I keep getting a warning, "number of items to replace is not a multiple of replacement length". And the data produced doesn't contain all 49 columns.
Sorry if this is a basic question or has already been solved, I am a beginner. Thank you in advance!
r loops matrix add rows
r loops matrix add rows
asked Nov 12 '18 at 16:56
Bobbie CattaniBobbie Cattani
1
1
1
Possible duplicate of To get the sum for every 2 rows in R
– ANG
Nov 12 '18 at 17:21
add a comment |
1
Possible duplicate of To get the sum for every 2 rows in R
– ANG
Nov 12 '18 at 17:21
1
1
Possible duplicate of To get the sum for every 2 rows in R
– ANG
Nov 12 '18 at 17:21
Possible duplicate of To get the sum for every 2 rows in R
– ANG
Nov 12 '18 at 17:21
add a comment |
2 Answers
2
active
oldest
votes
This is probably a duplicate, but I couldn't locate a great match quickly, so here goes...you can use rowsum
:
my_matrix <- structure(c(0L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 1L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L), .Dim = c(10L, 4L), .Dimnames = list(NULL, NULL))
rowsum(my_matrix, as.integer(gl(nrow(my_matrix), 2, nrow(my_matrix))))
# [,1] [,2] [,3] [,4]
# 1 2 13 23 33
# 2 7 27 47 67
# 3 11 31 51 71
# 4 15 35 55 75
# 5 19 39 59 79
add a comment |
In the future, make sure to provide a minimal reproducible example of your data to make it easier for us to follow! You can show how you created dummy data (see my example below) or use something like dput()
to produce a text version of the data.
There are few things that I notice:
- You need to make your output matrix the appropriate size if you're going to be adding by reference like that. So something like
matrix(nrow = 500, ncol = 49)
- If you have 1000 rows and you want to add every other together, you actually only want to do the addition 500 times. Your
X
has length of 1000, so it will try to find rows that are higher than the largest row index inpopulation
. Take a look attail(X)
and you will see that it goes up to 1999. - Finally, you are traversing a vector of every other number (1, 3, 5, etc.) and also using that as your row index, so the matrix that you are trying to populate will have blank rows every other, which I don't think is what you want. (Correct me if I'm wrong).
Here is a small example with some dummy data that I made. The notable changes compared to your example are that I:
- Made the output matrix have the numbers of rows and columns that I desired (same number of columns as
population
and half as many rows) - Made X be half as long as
nrow(population)
- Use
1:length(X)
instead of1:X
in my for loop
Example:
population <- matrix(1:400, byrow = F, ncol = 4)
sum <- matrix(nrow = nrow(population)/2, ncol = 4)
X <- seq(1, by = 2, len = nrow(population)/2)
for (i in 1:length(X))
sum[i,] <- population[ X[i] ] + population[ X[i]+1 ]
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%2f53266780%2fhow-to-add-sum-rows-of-a-table-or-matrix-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
This is probably a duplicate, but I couldn't locate a great match quickly, so here goes...you can use rowsum
:
my_matrix <- structure(c(0L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 1L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L), .Dim = c(10L, 4L), .Dimnames = list(NULL, NULL))
rowsum(my_matrix, as.integer(gl(nrow(my_matrix), 2, nrow(my_matrix))))
# [,1] [,2] [,3] [,4]
# 1 2 13 23 33
# 2 7 27 47 67
# 3 11 31 51 71
# 4 15 35 55 75
# 5 19 39 59 79
add a comment |
This is probably a duplicate, but I couldn't locate a great match quickly, so here goes...you can use rowsum
:
my_matrix <- structure(c(0L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 1L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L), .Dim = c(10L, 4L), .Dimnames = list(NULL, NULL))
rowsum(my_matrix, as.integer(gl(nrow(my_matrix), 2, nrow(my_matrix))))
# [,1] [,2] [,3] [,4]
# 1 2 13 23 33
# 2 7 27 47 67
# 3 11 31 51 71
# 4 15 35 55 75
# 5 19 39 59 79
add a comment |
This is probably a duplicate, but I couldn't locate a great match quickly, so here goes...you can use rowsum
:
my_matrix <- structure(c(0L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 1L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L), .Dim = c(10L, 4L), .Dimnames = list(NULL, NULL))
rowsum(my_matrix, as.integer(gl(nrow(my_matrix), 2, nrow(my_matrix))))
# [,1] [,2] [,3] [,4]
# 1 2 13 23 33
# 2 7 27 47 67
# 3 11 31 51 71
# 4 15 35 55 75
# 5 19 39 59 79
This is probably a duplicate, but I couldn't locate a great match quickly, so here goes...you can use rowsum
:
my_matrix <- structure(c(0L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 1L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L), .Dim = c(10L, 4L), .Dimnames = list(NULL, NULL))
rowsum(my_matrix, as.integer(gl(nrow(my_matrix), 2, nrow(my_matrix))))
# [,1] [,2] [,3] [,4]
# 1 2 13 23 33
# 2 7 27 47 67
# 3 11 31 51 71
# 4 15 35 55 75
# 5 19 39 59 79
answered Nov 12 '18 at 17:05
JasonAizkalnsJasonAizkalns
10.9k42978
10.9k42978
add a comment |
add a comment |
In the future, make sure to provide a minimal reproducible example of your data to make it easier for us to follow! You can show how you created dummy data (see my example below) or use something like dput()
to produce a text version of the data.
There are few things that I notice:
- You need to make your output matrix the appropriate size if you're going to be adding by reference like that. So something like
matrix(nrow = 500, ncol = 49)
- If you have 1000 rows and you want to add every other together, you actually only want to do the addition 500 times. Your
X
has length of 1000, so it will try to find rows that are higher than the largest row index inpopulation
. Take a look attail(X)
and you will see that it goes up to 1999. - Finally, you are traversing a vector of every other number (1, 3, 5, etc.) and also using that as your row index, so the matrix that you are trying to populate will have blank rows every other, which I don't think is what you want. (Correct me if I'm wrong).
Here is a small example with some dummy data that I made. The notable changes compared to your example are that I:
- Made the output matrix have the numbers of rows and columns that I desired (same number of columns as
population
and half as many rows) - Made X be half as long as
nrow(population)
- Use
1:length(X)
instead of1:X
in my for loop
Example:
population <- matrix(1:400, byrow = F, ncol = 4)
sum <- matrix(nrow = nrow(population)/2, ncol = 4)
X <- seq(1, by = 2, len = nrow(population)/2)
for (i in 1:length(X))
sum[i,] <- population[ X[i] ] + population[ X[i]+1 ]
add a comment |
In the future, make sure to provide a minimal reproducible example of your data to make it easier for us to follow! You can show how you created dummy data (see my example below) or use something like dput()
to produce a text version of the data.
There are few things that I notice:
- You need to make your output matrix the appropriate size if you're going to be adding by reference like that. So something like
matrix(nrow = 500, ncol = 49)
- If you have 1000 rows and you want to add every other together, you actually only want to do the addition 500 times. Your
X
has length of 1000, so it will try to find rows that are higher than the largest row index inpopulation
. Take a look attail(X)
and you will see that it goes up to 1999. - Finally, you are traversing a vector of every other number (1, 3, 5, etc.) and also using that as your row index, so the matrix that you are trying to populate will have blank rows every other, which I don't think is what you want. (Correct me if I'm wrong).
Here is a small example with some dummy data that I made. The notable changes compared to your example are that I:
- Made the output matrix have the numbers of rows and columns that I desired (same number of columns as
population
and half as many rows) - Made X be half as long as
nrow(population)
- Use
1:length(X)
instead of1:X
in my for loop
Example:
population <- matrix(1:400, byrow = F, ncol = 4)
sum <- matrix(nrow = nrow(population)/2, ncol = 4)
X <- seq(1, by = 2, len = nrow(population)/2)
for (i in 1:length(X))
sum[i,] <- population[ X[i] ] + population[ X[i]+1 ]
add a comment |
In the future, make sure to provide a minimal reproducible example of your data to make it easier for us to follow! You can show how you created dummy data (see my example below) or use something like dput()
to produce a text version of the data.
There are few things that I notice:
- You need to make your output matrix the appropriate size if you're going to be adding by reference like that. So something like
matrix(nrow = 500, ncol = 49)
- If you have 1000 rows and you want to add every other together, you actually only want to do the addition 500 times. Your
X
has length of 1000, so it will try to find rows that are higher than the largest row index inpopulation
. Take a look attail(X)
and you will see that it goes up to 1999. - Finally, you are traversing a vector of every other number (1, 3, 5, etc.) and also using that as your row index, so the matrix that you are trying to populate will have blank rows every other, which I don't think is what you want. (Correct me if I'm wrong).
Here is a small example with some dummy data that I made. The notable changes compared to your example are that I:
- Made the output matrix have the numbers of rows and columns that I desired (same number of columns as
population
and half as many rows) - Made X be half as long as
nrow(population)
- Use
1:length(X)
instead of1:X
in my for loop
Example:
population <- matrix(1:400, byrow = F, ncol = 4)
sum <- matrix(nrow = nrow(population)/2, ncol = 4)
X <- seq(1, by = 2, len = nrow(population)/2)
for (i in 1:length(X))
sum[i,] <- population[ X[i] ] + population[ X[i]+1 ]
In the future, make sure to provide a minimal reproducible example of your data to make it easier for us to follow! You can show how you created dummy data (see my example below) or use something like dput()
to produce a text version of the data.
There are few things that I notice:
- You need to make your output matrix the appropriate size if you're going to be adding by reference like that. So something like
matrix(nrow = 500, ncol = 49)
- If you have 1000 rows and you want to add every other together, you actually only want to do the addition 500 times. Your
X
has length of 1000, so it will try to find rows that are higher than the largest row index inpopulation
. Take a look attail(X)
and you will see that it goes up to 1999. - Finally, you are traversing a vector of every other number (1, 3, 5, etc.) and also using that as your row index, so the matrix that you are trying to populate will have blank rows every other, which I don't think is what you want. (Correct me if I'm wrong).
Here is a small example with some dummy data that I made. The notable changes compared to your example are that I:
- Made the output matrix have the numbers of rows and columns that I desired (same number of columns as
population
and half as many rows) - Made X be half as long as
nrow(population)
- Use
1:length(X)
instead of1:X
in my for loop
Example:
population <- matrix(1:400, byrow = F, ncol = 4)
sum <- matrix(nrow = nrow(population)/2, ncol = 4)
X <- seq(1, by = 2, len = nrow(population)/2)
for (i in 1:length(X))
sum[i,] <- population[ X[i] ] + population[ X[i]+1 ]
answered Nov 12 '18 at 17:18
QwfqwfQwfqwf
30818
30818
add a comment |
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%2f53266780%2fhow-to-add-sum-rows-of-a-table-or-matrix-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
1
Possible duplicate of To get the sum for every 2 rows in R
– ANG
Nov 12 '18 at 17:21