On Aug 29, 2010, at 8:47 PM, Lorenzo Cattarino wrote:

Hi,



I have the following matrix



cc <- matrix (1:21, 3)

cc[,3:4]<- 0

cc

    [,1] [,2] [,3] [,4] [,5] [,6] [,7]

[1,]    1    4    0    0   13   16   19

[2,]    2    5    0    0   14   17   20

[3,]    3    6    0    0   15   18   21



and I would like to sum just the values in columns 2, 3 and 4, so to
have something like



1  4  13  16  19

2  5  14  17  20

3  6  15  18  21

To take only selected columns:

> CC <-cc[,c(1,2,5,6,7)]
> CC
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4   13   16   19
[2,]    2    5   14   17   20
[3,]    3    6   15   18   21

I'm having difficulty understanding the point of summing columns which you have just set to zero so am taking the liberty of changing the task to summing columns 2-4 and leaving in column 2 of the shortened matrix:

> CC[,2] <-apply(cc[ , 2:4], 1, sum)
> CC
     [,1] [,2] [,3] [,4] [,5]
[1,]    1   21   13   16   19
[2,]    2   24   14   17   20
[3,]    3   27   15   18   21



Thanks



Lorenzo


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to