Sum columns of an array per row and place the total in a new column

Asked

Viewed 66 times

1

I have a 5x5 matrix with numerical values, as in the example below:

Col1   Col2   Col3   Col4   Col5
 1      2      3      4      5
 1      2      3      4      5
 1      2      3      4      5
 1      2      3      4      5
 1      2      3      4      5

I need to create a new column in that matrix containing the sum of columns row by row, as in the example below:

Col1   Col2   Col3   Col4   Col5   SOMA
 1      2      3      4      5      15
 1      2      3      4      5      15
 1      2      3      4      5      15
 1      2      3      4      5      15
 1      2      3      4      5      15
  • Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of this. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English (short version). If the help is very simple it is still possible to do in the comments.

  • It would be important for you to present some of the data structure, but by the way, you can try to help. # Create a new column: dataframe$NOME_DA_COLUNA <- dataframe[1] + dataframe[2] + dataframe[3] + dataframe[4] + dataframe[5]

  • 1

    base$SOMA <- rowSums(base). That’s it. But this table is more like a class object "data.frame" with a class object "matrix". If it’s a matrix, base <- cbind(base, SOMA = rowSums(base)).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.