Hi, Is there a function which operates on a matrix and return a vector of min/max of each rol/col ? say, X= 2, 1 3, 4 min.col(X)=c(2,1) thanks a lot. tong
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 On 6 Sep 2006, at 09:37, Tong Wang wrote:> Hi, > Is there a function which operates on a matrix and return a > vector of min/max of each rol/col ? > say, X= 2, 1 > 3, 4 > min.col(X)=c(2,1) > > thanks a lot. > > tong > > ______________________________________________ > R-help at 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.-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
you could use something like: # for row min and max apply(X, 1, min) apply(X, 1, max) # for column min and max apply(X, 2, min) apply(X, 2, max) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Tong Wang" <wangtong at usc.edu> To: "R help" <r-help at stat.math.ethz.ch> Sent: Wednesday, September 06, 2006 10:37 AM Subject: [R] What is the matrix version of min()> Hi, > Is there a function which operates on a matrix and return a > vector of min/max of each rol/col ? > say, X= 2, 1 > 3, 4 > min.col(X)=c(2,1) > > thanks a lot. > > tong > > ______________________________________________ > R-help at 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Tong Wang a ?crit :> Hi, > Is there a function which operates on a matrix and return a vector of min/max of each rol/col ? > say, X= 2, 1 > 3, 4 > min.col(X)=c(2,1) > thanks a lot. > tongsee ?pmin, which.min, which.max, max.col hih
see ?apply min.row <- apply(X, 1, min) min.col <- apply(X, 2, min) JeeBee On Wed, 06 Sep 2006 01:37:22 -0700, Tong Wang wrote:> Hi, > Is there a function which operates on a matrix and return a vector of > min/max of each rol/col ? > say, X= 2, 1 > 3, 4 > min.col(X)=c(2,1) > > thanks a lot. > > tong > > ______________________________________________ R-help at 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.
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
Hi, THANK YOU ALL for the prompt reply. cheers. ----- Original Message ----- From: Robin Hankin <r.hankin at noc.soton.ac.uk> Date: Wednesday, September 6, 2006 1:42 am Subject: Re: [R] What is the matrix version of min() To: Tong Wang <wangtong at usc.edu> Cc: R help <r-help at stat.math.ethz.ch>> 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 > > > On 6 Sep 2006, at 09:37, Tong Wang wrote: > > > Hi, > > Is there a function which operates on a matrix and return a > > vector of min/max of each rol/col ? > > say, X= 2, 1 > > 3, 4 > > min.col(X)=c(2,1) > > > > thanks a lot. > > > > tong > > > > ______________________________________________ > > R-help at 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. > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > > > >