Hi @ all, how do I get the largest or lowest k values of list? It must similar to the min() / max() function, but I just don't get it. Best wishes, Markus
Lowest k values of list
6 messages · Markus Mühlbacher, Jorge Ivan Velez, David Winsemius +2 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080905/a574c093/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080905/c378e7f9/attachment.pl>
On Sep 5, 2008, at 11:26 AM, Markus M?hlbacher wrote:
Hi @ all, how do I get the largest or lowest k values of list? It must similar to the min() / max() function, but I just don't get it.
Might depend on how you define "high" and "low". Consider these experiments: > z <- list(1,4,"5",6,,2,3) > head(sort(unlist(z)),n=2) [1] "1" "2" > tail(sort(unlist(z)),n=2) [1] "5" "6" > z <- list(1,2,3,4,"5",6,"a") > tail(sort(unlist(z)),n=2) [1] "6" "a" > head(sort(unlist(z)),n=2) [1] "1" "2"
David Winsemius
Do you mean a vector? If so, head/tail will work for you:
x <- runif(10) x
[1] 0.26550866 0.37212390 0.57285336 0.90820779 0.20168193 0.89838968 0.94467527 [8] 0.66079779 0.62911404 0.06178627
# lowest 5 head(sort(x), 5)
[1] 0.06178627 0.20168193 0.26550866 0.37212390 0.57285336
# highest 5 tail(sort(x), 5)
[1] 0.6291140 0.6607978 0.8983897 0.9082078 0.9446753
On Fri, Sep 5, 2008 at 11:26 AM, Markus M?hlbacher <muehliman at yahoo.com> wrote:
Hi @ all, how do I get the largest or lowest k values of list? It must similar to the min() / max() function, but I just don't get it. Best wishes, Markus
______________________________________________ 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?
In addition to the other responses, you may want to look at the 'partial' argument to sort. For large vectors it may speed things up to not sort everything if all you care about is the top and bottom few. Try:
tmp <- rnorm(100) plot( sort(tmp, partial=c(1,2,99,100) ) )
Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org (801) 408-8111
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Markus M?hlbacher Sent: Friday, September 05, 2008 9:26 AM To: r-help at r-project.org Subject: [R] Lowest k values of list Hi @ all, how do I get the largest or lowest k values of list? It must similar to the min() / max() function, but I just don't get it. Best wishes, Markus
______________________________________________ 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.