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