[R] RE : how to calculate the average values of each row in a matrix

Guibert TCHINDE gtchinde at aid.fr
Sat Jul 20 09:42:29 CEST 2013


> vec = matrix(data=round(rnorm(15,1,2)),ncol=3)
> vec
     [,1] [,2] [,3]
[1,]    1    1   -1
[2,]    6    2   -1
[3,]    0    0    2
[4,]    0   -3   -2
[5,]    1    1    2
> rowMeans(vec)
[1]  0.3333333  2.3333333  0.6666667 -1.6666667  1.3333333
> apply(vec,1,mean)
[1]  0.3333333  2.3333333  0.6666667 -1.6666667  1.3333333

may be this helps

GT
________________________________________
De : r-help-bounces at r-project.org [r-help-bounces at r-project.org] de la part de R. Michael Weylandt <michael.weylandt at gmail.com> [michael.weylandt at gmail.com]
Date d'envoi : samedi 20 juillet 2013 03:33
À : Elaine Kuo
Cc : r-help at stat.math.ethz.ch
Objet : Re: [R] how to calculate the average values of each row in a matrix

On Jul 19, 2013, at 20:19, Elaine Kuo <elaine.kuo.tw at gmail.com> wrote:

> Hello,
>
> I have a matrix (class matrix) composed of GridCell (row and column).
> The matrix value is the beta diversity index value between two grids.
>
> Now I would like to get the average value of each GridCell.
> Please kindly advise how to make the calculation.
> Thank you.
>

Perhaps the rowMeans() function?

MW

> Elaine
>
> The matrix looks like (cited from Michael Friendly)
> I would like to get the average value of each color.
>
>
>      Obs  stim   RPur   Red   Yel   Gy1   Gy2  Green  Blue  BlP  Pur1
> Pur2
>
>                      1  RPur     .     .     .     .     .      .     .
> .     .     .
>                      2  Red    11.5    .     .     .     .      .     .
> .     .     .
>                      3  Yel    13.1   6.0    .     .     .      .     .
> .     .     .
>                      4  Gy1    12.6   7.9   6.2    .     .      .     .
> .     .     .
>                      5  Gy2    10.6   8.4   8.4   5.2    .      .     .
> .     .     .
>                      6  Green  10.6   9.4   9.9   6.5   4.1     .     .
> .     .     .
>                      7  Blue   10.8  10.2  10.3   8.8   7.0    6.4    .
> .     .     .
>                       8  BlP     7.3  11.3  12.7  11.2  10.4    9.9   4.2
>  .     .     .
>                       9  Pur1    5.4  11.5  12.9  11.7  10.8    9.4   8.4
> 4.5    .     .
>                      10  Pur2    5.0  11.5  10.7  10.2  10.6   10.1   8.1
> 6.4    3     .
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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 at 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.


Click https://www.mailcontrol.com/sr/0NMwdyLXvWXGX2PQPOmvUlvAwUAcTfZn4fkKLlvv9q+YH6kYUkFzXm6zZ2oqmDGRhRWtQ2HzzdeVGm43FW9a3A==  to report this email as spam.


More information about the R-help mailing list