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
Dear Markus, Try this: # Data set set.seed(123) mylist=list(a=rnorm(10),b=rpois(15,10),f=rnorm(30,12,3)) mylist # min and max temp=lapply(mylist,function(x){ res=c(min(x),max(x)) names(res)=c('Min.','Max.') res } ) do.call(rbind,temp) Min. Max. a -1.265061 1.715065 b 3.000000 15.000000 f 7.353742 18.506868 HTH, Jorge On Fri, Sep 5, 2008 at 11:26 AM, Markus Mühlbacher <muehliman@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@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]]
Dear Markus, I think that I missed something in my previous reply. Here is another approach using the first k values of each list: # Data set set.seed(123) mylist=list(a=rnorm(10),b=rpois(15,10),f=rnorm(30,12,3)) # Function to report the first k values k.values=function(x,k){ res= x[order(x)][1:k] names(res)=paste('value_',1:k,sep="") res } # Example: first 3 values res=lapply(mylist,k.values,k=3) do.call(rbind,res) value_1 value_2 value_3 a -1.265061 -0.6868529 -0.5604756 b 3.000000 4.0000000 6.0000000 f 7.353742 8.2038109 8.5855892 HTH, Jorge On Fri, Sep 5, 2008 at 11:26 AM, Markus Mühlbacher <muehliman@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@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]]
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. >