I would like to list the top n entries in a dataframe for each level of a factor or an integer. Here is the code I have:

```
index <- c(1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3)
prob <- runif(20,0,1)

x.df <- data.frame(cbind(index,prob))
x.df <- x.df[order(x.df$index,-x.df$prob),]

head(x.df[x.df$index==1,],n=3)
head(x.df[x.df$index==2,],n=3)
head(x.df[x.df$index==3,],n=3)

```

This does the job, but I'd like to not have to explicitly have a head statement for every level/integer. Thanks.