[R] Easy way to get top 2 items from vector
jim holtman
jholtman at gmail.com
Sat Sep 5 22:56:04 CEST 2009
Tyr this:
> system.time(sort(x, partial=c(999999,1000000)))
user system elapsed
0.12 0.00 0.14
On Fri, Sep 4, 2009 at 3:53 AM, ONKELINX,
Thierry<Thierry.ONKELINX at inbo.be> wrote:
> Using tail() for the selection is more elegant and slightly faster.
>
>> N<- 1000000
>> x <- runif(N)
>> system.time(x[order(x)[c(N-1,N)]])
> user system elapsed
> 1.08 0.01 1.10
>> system.time(sort(x)[c(N-1,N)])
> user system elapsed
> 0.36 0.00 0.35
>> system.time(tail(sort(x), 2))
> user system elapsed
> 0.33 0.00 0.33
>
>
> HTH,
>
> Thierry
>
> ----------------------------------------------------------------------------
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
> Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance
> Gaverstraat 4
> 9500 Geraardsbergen
> Belgium
> tel. + 32 54/436 185
> Thierry.Onkelinx at inbo.be
> www.inbo.be
>
> To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
> ~ Sir Ronald Aylmer Fisher
>
> The plural of anecdote is not data.
> ~ Roger Brinner
>
> The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
> -----Oorspronkelijk bericht-----
> Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens andrew
> Verzonden: vrijdag 4 september 2009 4:24
> Aan: r-help at r-project.org
> Onderwerp: Re: [R] Easy way to get top 2 items from vector
>
> it is speedier to use sort than a combination of [] and order:
>
> N<- 1000000
> x <- runif(N)
>> system.time(x[order(x)[c(N-1,N)]])
> user system elapsed
> 1.03 0.00 1.03
>> system.time(sort(x)[c(N-1,N)])
> user system elapsed
> 0.28 0.00 0.28
>
>
>
> On Sep 4, 11:17 am, Noah Silverman <n... at smartmediacorp.com> wrote:
>> Phil,
>>
>> That's perfect. (For my application, I've never seen a tie. While
>> possible, the likelihood is almost none.)
>>
>> Thanks!
>>
>> --
>> Noah
>>
>> On 9/3/09 4:29 PM, Phil Spector wrote:
>>
>>
>>
>> > Noah -
>> > max(x[-which.max(x)] will give you the second largest value, but
>> > it doesn't handle ties.
>> > x[order(x,decreasing=TRUE)[n]] will give you the nth largest
>> > value, with the same caveat regarding ties. For example,
>> > x[order(x,decreasing=TRUE)[1:3]] will give you the three largest
>> > values.
>>
>> > - Phil Spector
>> > Statistical Computing Facility
>> > Department of Statistics
>> > UC Berkeley
>> > spec... at stat.berkeley.edu
>>
>> > On Thu, 3 Sep 2009, Noah Silverman wrote:
>>
>> >> Hi,
>>
>> >> I use the max function often to find the top value from a matrix or
>> >> column of a data.frame.
>>
>> >> Now I'm looking to find the top 2 (or three) values from my data.
>>
>> >> I know that I could sort the list and then access the first two
>> >> items, but that seems like the "long way". Is there some way to
>> >> access "max_2" or similar?
>>
>> >> Thanks!
>>
>> >> --
>> >> Noah
>>
>> >> ______________________________________________
>> >> R-h... 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.
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-h... at r-project.org mailing
>> listhttps://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting
>> guidehttp://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> 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.
>
> Druk dit bericht a.u.b. niet onnodig af.
> Please do not print this message unnecessarily.
>
> Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
> en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
> door een geldig ondertekend document. The views expressed in this message
> and any annex are purely those of the writer and may not be regarded as stating
> an official position of INBO, as long as the message is not confirmed by a duly
> signed document.
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list