dear group, i have a data matrix with some replicate items with different values. I want to extract the row with max value. for example:> xfruit weight 1 apple 1.3 2 apple 1.5 3 apple 1.6 4 orange 1.4 5 orange 1.6 x is a data frame. I want to extract unique items from fruits that has max weight. that is: 3 apple 1.6 5 orange 1.6 I want to be able to use apply functions. Could some one lend some help please. Thanks srini
Dear Srini, Here is one way: # Data set x=read.table(textConnection("fruit weight 1 apple 1.3 2 apple 1.5 3 apple 1.6 4 orange 1.4 5 orange 1.6"),header=TRUE) x[tapply(x$weight,x$fruit,which.max),] apple orange 1.6 1.6 or Try also x[cumsum(tapply(x$weight,x$fruit,which.max)),] fruit weight 3 apple 1.6 5 orange 1.6 HTH, Jorge On Sun, Sep 7, 2008 at 10:24 PM, Srinivas Iyyer <srini_iyyer_bio@yahoo.com>wrote:> dear group, > i have a data matrix with some replicate items with different values. I > want to extract the row with max value. > > for example: > > x > fruit weight > 1 apple 1.3 > 2 apple 1.5 > 3 apple 1.6 > 4 orange 1.4 > 5 orange 1.6 > > > x is a data frame. > I want to extract unique items from fruits that has max weight. > > that is: > > 3 apple 1.6 > 5 orange 1.6 > > I want to be able to use apply functions. Could some one lend some help > please. > > Thanks > srini > > ______________________________________________ > R-help@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]]
> aggregate(x$weight,list(x$fruit),max)Group.1 x 1 apple 1.6 2 orange 1.6 Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Srinivas Iyyer Sent: Monday, September 08, 2008 7:55 AM To: r-help at stat.math.ethz.ch Subject: [R] extracting max row from data matrix dear group, i have a data matrix with some replicate items with different values. I want to extract the row with max value. for example:> xfruit weight 1 apple 1.3 2 apple 1.5 3 apple 1.6 4 orange 1.4 5 orange 1.6 x is a data frame. I want to extract unique items from fruits that has max weight. that is: 3 apple 1.6 5 orange 1.6 I want to be able to use apply functions. Could some one lend some help please. Thanks srini ______________________________________________ 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. This e-mail may contain confidential and/or privileged i...{{dropped:10}}
Hi Srini, This may be as simple as tapply(weight,fruit,max) or t(that) if you want it as you specified. --Adam On Sun, 7 Sep 2008, Srinivas Iyyer wrote:> dear group, > i have a data matrix with some replicate items with different values. I want to extract the row with max value. > > for example: >> x > fruit weight > 1 apple 1.3 > 2 apple 1.5 > 3 apple 1.6 > 4 orange 1.4 > 5 orange 1.6 > > > x is a data frame. > I want to extract unique items from fruits that has max weight. > > that is: > > 3 apple 1.6 > 5 orange 1.6 > > I want to be able to use apply functions. Could some one lend some help please. > > Thanks > srini > > ______________________________________________ > 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. >