[R] (no subject)

jim holtman jholtman at gmail.com
Fri Dec 10 15:42:44 CET 2010


try this:

> do.call(rbind, lapply(split(x, x$X1), function(.grp){
+     .ord <- .grp[order(.grp$X2, decreasing = TRUE),]
+     .ord[seq(min(5, nrow(.grp))),]
+ }))
     X1 X2
1.10  1 10
1.9   1  9
1.8   1  8
1.7   1  7
1.6   1  6
2.25  2 25
2.24  2 24
2.23  2 23
2.22  2 22
2.21  2 21


On Fri, Dec 10, 2010 at 9:18 AM, andrija djurovic <djandrija at gmail.com> wrote:
> Hi R-help,
>
>
>
> I am trying to find a way to select five highest values in data frame
> according some variable. I will demonstrate:
>
> c
>
>   X1 X2
>
> 1   1  1
>
> 2   1  2
>
> 3   1  3
>
> 4   1  4
>
> 5   1  5
>
> 6   1  6
>
> 7   1  7
>
> 8   1  8
>
> 9   1  9
>
> 10  1 10
>
> 11  2 11
>
> 12  2 12
>
> 13  2 13
>
> 14  2 14
>
> 15  2 15
>
> 16  2 16
>
> 17  2 17
>
> 18  2 18
>
> 19  2 19
>
> 20  2 20
>
> 21  2 21
>
> 22  2 22
>
> 23  2 23
>
> 24  2 24
>
> 25  2 25
>
>
>
> So I would like to select a rows with higest values of X2 inside X1.
> Expected result should be:
>
> X1 X2
>
>   1  10
>
>   1  9
>
>   1  8
>
>   1  7
>
>   1  6
>
>   2  25
>
>   2  24
>
>   2  23
>
>   2  22
>
>   2  21
>
>
>
> I first oreded the data frame using
>
> c=c[with(c,order(X1,-X2)),]
>
> but I need a help to select highes five. It is easy to select when I have
> just 2 unique values of X1 but what is if I have 500 unique values in X1?
>
>
>
> Thanks
>
> Andrija
>
>        [[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
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list