Very nice, Phil. Thank you.

-----Original Message-----
From: Phil Spector [mailto:spec...@stat.berkeley.edu] 
Sent: Monday, September 20, 2010 1:28 PM
To: Doran, Harold
Cc: R-help
Subject: Re: [R] Sorting and subsetting

Harold -
    Two ways that come to mind:

1) do.call(rbind,lapply(split(tmp,tmp$index),function(x)x[1:5,]))
2) subset(tmp,unlist(tapply(foo,index,seq))<=5)

                                        - Phil Spector
                                         Statistical Computing Facility
                                         Department of Statistics
                                         UC Berkeley
                                         spec...@stat.berkeley.edu



On Mon, 20 Sep 2010, Doran, Harold wrote:

> Suppose I have a data frame, such as the one below:
>
> tmp <- data.frame(index = gl(2,20), foo = rnorm(40))
>
> And further assume it is sorted by index and then by the variable foo.
>
> tmp <- tmp[order(tmp$index, tmp$foo) , ]
>
> Now, I want to grab the first N rows of tmp for each index. In the end, what 
> I want is the data frame 'result'
>
> tmp1 <- subset(tmp, index == 1)
> tmp2 <- subset(tmp, index == 2)
>
> tmp1 <- tmp1[1:5,]
> tmp2 <- tmp2[1:5,]
> result <- rbind(tmp1, tmp2)
>
> Does anyone see a way to subset and subsequently bind without a loop?
>
> Harold
>
>
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

______________________________________________
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.

Reply via email to