[R] Sorting and subsetting

Doran, Harold HDoran at air.org
Mon Sep 20 19:29:35 CEST 2010


Very nice, Phil. Thank you.

-----Original Message-----
From: Phil Spector [mailto:spector at 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
 					 spector at 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 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.
>



More information about the R-help mailing list