[R] subsetting a data.frame

jim holtman jholtman at gmail.com
Wed Oct 10 14:40:19 CEST 2007


Is this what you want?

> x <- read.table(textConnection("Score     Name
+ 88           000019_0070
+ 88           000019_0070
+ 87           000019_0070
+ 79           002127_0658
+ 79           002127_0658
+ 77           002127_0658"), header=TRUE)
> # return best scores
> best <- by(x, x$Name, function(.nam){
+     .nam[which(.nam$Score == max(.nam$Score)),]
+ })
> do.call('rbind', best)
              Score        Name
000019_0070.1    88 000019_0070
000019_0070.2    88 000019_0070
002127_0658.4    79 002127_0658
002127_0658.5    79 002127_0658
>


On 10/10/07, João Fadista <Joao.Fadista at agrsci.dk> wrote:
> Dear all,
>
> I would like to be able to subset a data.frame in a special way. I will put here an example:
>
> Score     Name
> 88           000019_0070
> 88           000019_0070
> 87           000019_0070
> 79           002127_0658
> 79           002127_0658
> 77           002127_0658
>
> So, for the above example I would like to have a new data.frame that has only the best "Score" for each "Name":
>
> Score     Name
> 88           000019_0070
> 88           000019_0070
> 79           002127_0658
> 79           002127_0658
>
>
>
> Thanks in advance.
> Best regards,
> João Fadista
>
>
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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 you are trying to solve?



More information about the R-help mailing list