[R] Find the three best values in every row

Alfred-Schulze at web.de Alfred-Schulze at web.de
Fri May 7 18:34:07 CEST 2010


-----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, UKtt




Thanks, 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



More information about the R-help mailing list