Re: [R] What is the matrix version of min()

From: David Scott <d.scott_at_auckland.ac.nz>
Date: Wed 06 Sep 2006 - 09:09:17 GMT

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@auckland.ac.nz

______________________________________________
R-help@stat.math.ethz.ch 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. Received on Wed Sep 06 19:12:30 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 07 Sep 2006 - 07:51:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.