Or perhaps use a temporary vector might be neater? tmp <- with(df.mydata, B[A=="X" & C < 2]) df.mydata[order(tmp) %in% 1:3,] # gives df with highest three values of B
or head(df.mydata[order(tmp),],3) # gives first 3 rows of df sorted by B Colin. -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Mohamed Lajnef Sent: 26 August 2009 11:25 To: Noah Silverman Cc: r help Subject: Re: [R] Select top three values from data frame Noah Silverman a écrit : > I only have a few values in my example, but the real data set might have > 20-100 rows with A="X". So how do I pick just the three highest ones? > > -N > > Hi, and now? df.mydata$B[order(df.mydata[df.mydata$A=="X" AND df.mydata$C < 2, ]$B)][length(df.mydata$B)-3:length(df.mydata$B)] cheers, ML > On 8/26/09 2:46 AM, Ottorino-Luca Pantani wrote: > >> df.mydata[df.mydata$A=="X" AND df.mydata$C < 2, ] >> will do the job ? >> >> 8rino >> >> Noah Silverman ha scritto: >> >>> Hi, >>> >>> I'm trying to find an easy way to do this. >>> >>> I want to select the top three values of a specific column in a >>> subset of rows in a data.frame. I'll demonstrate. >>> >>> A B C >>> x 2 1 >>> x 4 1 >>> x 3 2 >>> y 1 5 >>> y 2 6 >>> y 3 8 >>> >>> >>> I want the top 3 values of B from the data.frame where A=X and C <2 >>> >>> I could extract all the rows where C<2, then sort by B, then take the >>> first 3. But that seems like the wrong way around, and it also will >>> get messy with real data of over 100 columns. >>> >>> Any suggestions? >>> >>> > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > > -- Mohamed Lajnef INSERM Unité 955. 40 rue de Mesly. 94000 Créteil. Courriel : mohamed.laj...@inserm.fr tel. : 01 49 81 31 31 (poste 18470) Sec : 01 49 81 32 90 fax : 01 49 81 30 99 ______________________________________________ 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. ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________ 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.