[R] Sorting and subsetting

Peter Dalgaard pdalgd at gmail.com
Mon Sep 20 19:49:31 CEST 2010


On 09/20/2010 07:16 PM, Doran, Harold wrote:
> 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?
> 

> do.call(rbind,lapply(split(tmp,tmp$index),head,5))
     index        foo
1.11     1 -1.5124909
1.10     1 -1.3835811
1.20     1 -1.0906574
1.6      1 -0.8588022
1.8      1 -0.8384081
2.28     2 -2.9193984
2.36     2 -0.8782202
2.33     2 -0.7624129
2.38     2 -0.5995872
2.23     2 -0.5912392


(Sorry about the silly rownames.)

Or, (HACK ALERT!)
> tmp[ave(tmp$foo,tmp$index,FUN=seq_along)<=5,]
   index        foo
11     1 -1.5124909
10     1 -1.3835811
20     1 -1.0906574
6      1 -0.8588022
8      1 -0.8384081
28     2 -2.9193984
36     2 -0.8782202
33     2 -0.7624129
38     2 -0.5995872
23     2 -0.5912392

(The silly bit in this case being that you can only ave() a numeric
variable.)

Or maybe:

> tmp[unlist(tapply(seq_along(tmp$index), tmp$index, head,5)),]




-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list