[R] Summing over specific columns in a matrix

emj83 stp08emj at shef.ac.uk
Fri Jan 7 17:29:03 CET 2011


I would like to sum some specific columns in my matrix- for example, my
matrix looks like this:
     [,1]  [,2]  [,3] [,4]  [,5]
[1,]    1   NA   NA   NA   NA
[2,]    2    1   NA    1   NA
[3,]    3    2    1     2    1
[4,]    4    3    2     3    2
[5,]   NA   NA   NA    4    3
[6,]   NA   NA   NA    5   NA

I would like to find the sum of the first two columns, the second two
columns and the last column:
i.e I am left with a vector of c(16, 18, 6).

I know about colSums and sum overall- I just wondered if this type of
grouping can be included somehow in a vector such as c(2,2,1)? I don't
really want to have to use a loop for this.

Many thanks Emma
View this message in context: http://r.789695.n4.nabble.com/Summing-over-specific-columns-in-a-matrix-tp3179400p3179400.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list