Sum of columns into a new column

Good morning all,
I am new to R and have searched long enough for an answer to a fairly basic problem ...
I have a dataset with various variables. My columns 6 to 50 are particle volumes and I would just like to have a new column (hence a new variable) that is the sum of all the other columns:

For example: imagine that I have 4 columns A B C and D:

A B C D
0 1 1 2

I would then like to have a column E which for this row indicates 4

I hope I was pretty much clear ^^

thank you

?rowSums

mat1  <-  matrix(1:20, ncol = 4)
rs  <-  rowSums(mat1)
mat2  <-  cbind(mat1, rs)

Thanks a lot for your answer !
I don't understand what your 1:20 is and the ncol = 4?
I was thinking of using the mutate function to create a new variable which would be the sum of my columns.
Basically I have a basic data set called dataset1 with my variables 6 to 50 were volumes according to particle sizes, each variable has the mention LPM in its name. I just want to add all the volumes of each of the sizes to have only one variable and not 44

Sites LPM6 LPM7 ................ LPM49 LPM 50
1 1 0 ...................... 2 0
2 0 2 .......................1 0
3 1 2 .......................1 1
4 2 1 .......................0 1
.... .... .. ...................... .. ...

sites LPMtotal
1 3
2 3
3 4
4 4

I would like to have a column named LPMtotal which for each of my lines it makes me the sum of all the LPMs so there in my example 3

basically have a variable LPMtotal which is the sum of the LPM variables from column 6 to column 50

mat1 <- matrix(1:20, ncol = 4)
This just creates a matrix as sample data. I created a matrix of the numbers from 1 to 20 arranged in 4 rows. The ncol specifies the number of rows.
.

mat1
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20

It would help to have some data---see the FAQ posted below but if I understand you properly you just need to do:

rs  <-  rowSums(mydata[ , 6:44])  

and then do a cbind().

Here is a toy example with "dat1" as a dataframe.

dat1  <-  structure(list(rr = c("a", "b", "c", "d", "e"), X1 = 1:5, X2 = 6:10, 
    X3 = 11:15, X4 = 16:20), class = "data.frame", row.names = c(NA, 
-5L))


rs  <-  rowSums(dat1[ , 2 : 5])

dat2  <-  cbind(dat1, rs)

I do not usually use tidyverse so I don"t know if mutate() is the correct approach. My approach is simple basic R which will do the job. Note, if you have NAs in the data you will need to use

rowSums(mydata[ , 6:44], na.rm = TRUE)

Some suggestions on asking questions.

Good luck

Hello !
I was finally able to find out how to do it! I used mutate, thank you very much for your help, I was able to learn a lot about the different ways of using R:
if you are interested, here is how I proceeded:

x <- dataset2%>%
select (starts_with ("LPM bio"))

dataset3 <- mutate (dataset2, LPMtotal = rowSums (x))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.