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]]

