[R] Matrix- create mean/min/max/stdev on column of matrix or rows?

Henrik Bengtsson hb at stat.berkeley.edu
Sat Sep 18 00:34:04 CEST 2010


See also the matrixStats package on CRAN.

/Henrik

On Fri, Sep 17, 2010 at 10:17 AM, Dennis Murphy <djmuser at gmail.com> wrote:
> Hi:
>
> Here's a simple example that you can tune to your needs:
>
> m <- matrix(rpois(100, 10), nrow = 10)
>
> # Function to compute summary statistics:
> f <- function(x) c(min = min(x), med = median(x), mean = mean(x), max =
> max(x))
>
> # Apply to rows (index 1)
> apply(m, 1, f)
>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> min   5.0  7.0  5.0  6.0  5.0  5.0  8.0  5.0  2.0   6.0
> med  11.5  9.5 10.0  9.5  9.0  8.0 10.0 10.0  6.0  10.0
> mean 11.1  9.8 10.4 10.3  9.6  7.6 10.5  9.3  6.6   9.8
> max  15.0 14.0 17.0 15.0 17.0 10.0 14.0 14.0 16.0  12.0
>
> # Apply to columns (index 2)
> apply(m, 2, f)
>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> min   3.0  6.0  5.0  6.0  2.0  5.0  5.0  5.0  8.0   6.0
> med   9.0  9.5 11.0  9.5  8.5  9.5  9.0  6.0 10.5  10.0
> mean  8.6  9.6 10.8  9.8  8.5  9.7  9.7  7.8 11.0   9.5
> max  12.0 15.0 15.0 17.0 14.0 15.0 17.0 14.0 16.0  14.0
>
> HTH,
> Dennis
>
> On Fri, Sep 17, 2010 at 9:08 AM, Halabi, Anan <anan.halabi at hp.com> wrote:
>
>> I made simulation with Weibull and create Matrix,
>> How can I create mean/min/max/stdev on column or rows of matrix?,
>> Thanks,
>>
>> ______________________________________________
>> 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.
>>
>
>        [[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.
>



More information about the R-help mailing list