[R] What is the matrix version of min()
David Scott
d.scott at auckland.ac.nz
Wed Sep 6 11:09:17 CEST 2006
On Wed, 6 Sep 2006, Robin Hankin wrote:
> Tong
>
> you need to use apply(). The second argument specifies whether
> you want to work with rows or columns. The point of this is that
> min() and max() operate on vectors and give a single value,
> and you want to "apply" this function to all rows or all columns:
>
> > a <- matrix(rnorm(30),5,6)
> > apply(a,2,max)
> [1] 2.6413241 0.9842076 1.7989560 0.6999855 2.0542201 0.1162821
> > apply(a,1,max)
> [1] 1.1771370 0.9811693 2.6413241 0.9842076 2.0542201
> >
>
> HTH
>
> rksh
>
Or in some circumstances you can use pmin (or pmax):
> a<-matrix(rnorm(15),5,3)
> a
[,1] [,2] [,3]
[1,] 1.5175319 -0.4428964 -0.55473327
[2,] -0.2235937 1.0157411 0.08653748
[3,] 0.3240530 -0.4251498 -0.28565732
[4,] 0.4663556 1.1933213 0.60395935
[5,] 0.4078475 0.1739074 1.85645664
> pmin(a[,1],a[,2],a[,3])
[1] -0.5547333 -0.2235937 -0.4251498 0.4663556 0.1739074
David Scott
_____________________________________________________________
David Scott Visiting (July 06 to January 07)
Department of Probability and Statistics
The University of Sheffield
The Hicks Building
Hounsfield Road
Sheffield S3 7RH
United Kingdom
Phone: +44 114 222 3908
Email: d.scott at auckland.ac.nz
More information about the R-help
mailing list