Hello, i have a dataframe with the GDP for different Country (in the columns) and Years (in the rows). Now i want for every year the best three values, if possible with name of the countries (columnnames). For the best it's no problem but for the other two values. Thanks, Alfred
Hmm... set.seed(17*11) d<-data.frame(africa=sample(50, 10), europe= sample(50, 10), n.america= sample(50, 10), s.america= sample(50, 10), antarctica= sample((1:50)/20, 10) ) #Get three top from each row t(apply(d,1,function(x,n) x[which(rank(x)> length(x)-n)], n=3)) #Get them with names get.n<- function(x, names, n) { rmin<- rv<-x[w<-which(rank(x)> length(x)-n)] names(rv)<-names[w] return(as.data.frame(rv)) } apply(d,1,get.n, n=3, names=names(d))>>> Alfred Schulze <Alfred-Schulze at web.de> 05/07/10 2:43 PM >>>Hello, i have a dataframe with the GDP for different Country (in the columns) and Years (in the rows). Now i want for every year the best three values, if possible with name of the countries (columnnames). For the best it's no problem but for the other two values. Thanks, Alfred ______________________________________________ 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 email and any attachments are confidential. Any use...{{dropped:8}}
Hello Alfred, I found the solution from S. Ellison (https://stat.ethz.ch/pipermail/r-help/2010-May/238158.html) really inspiring. Here I am using tail and the library 'plyr': set.seed(17*11) d<-data.frame(africa=sample(50, 10), europe= sample(50, 10), n.america= sample(50, 10), s.america= sample(50, 10), antarctica= sample((1:50)/20, 10) ) # using tail t(apply(d, 1, function(x, n) tail(sort(x), n), n=3)) lapply(split(d, rownames(d)), function(x, n) sort(x)[n:ncol(x)], n=3) # with plyr from Hadley Wickham library(plyr) ldply(split(d, rownames(d)), function(x, n) sort(x)[n : ncol(x)], n=3) HTH Patrick Am 07.05.2010 15:43, schrieb Alfred Schulze:> > Hello, > > i have a dataframe with the GDP for different Country (in the columns) and > Years (in the rows). > > Now i want for every year the best three values, if possible with name of > the countries (columnnames). > > For the best it's no problem but for the other two values. > > Thanks, > > Alfred > ______________________________________________ > 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.
Alfred-Schulze at web.de
2010-May-07 16:34 UTC
[R] Find the three best values in every row
-----Urspr?ngliche Nachricht----- Von: S Ellison <S.Ellison at lgc.co.uk> Gesendet: 07.05.2010 16:44:08 An: r-help at r-project.org,Alfred-Schulze at web.de Betreff: Re: [R] Find the three best values in every row>Hmm...>> set.seed(17*11)> d<-data.frame(africa=sample(50, 10),> europe= sample(50, 10),> n.america= sample(50, 10),> s.america= sample(50, 10),> antarctica= sample((1:50)/20, 10)> )>>#Get three top from each row>t(apply(d,1,function(x,n) x[which(rank(x)> length(x)-n)], n=3))>>#Get them with names>get.n<- function(x, names, n) {> rmin<-> rv<-x[w<-which(rank(x)> length(x)-n)]> names(rv)<-names[w]> return(as.data.frame(rv))>}>>apply(d,1,get.n, n=3, names=names(d))>>>>>>> Alfred Schulze [ 05/07/10 2:43 PM >>>>> Hello,>> i have a dataframe with the GDP for different Country (in the>columns) and> Years (in the rows).>> Now i want for every year the best three values, if possible with>name of> the countries (columnnames).>> For the best it's no problem but for the other two values.>> Thanks,>> Alfred>______________________________________________>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 email and any attachments are confidential. Any use, copying or>disclosure other than by the intended recipient is unauthorised. If>you have received this message in error, please notify the sender>immediately via +44(0)20 8943 7000 or notify postmaster at lgc.co.uk>and delete this message and any copies from your computer and network.>LGC Limited. Registered in England 2991879.>Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UKttThanks, that's what I need. But I have some NA's in the data and t(apply(data.frame,1,function(x,n) x[which(rank(x)> length(x)-n)], n=3, na.rm=TRUE)) isn't working. And I have a further question. How I get the value also sorted descending? Thanks, Alfred ___________________________________________________________ GRATIS: Movie-Flat mit ?ber 300 Top-Videos. F?r WEB.DE Nutzer dauerhaft kostenlos! Jetzt freischalten unter http://movieflat.web.de