Let's say I have a data.frame like:
x <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
df <- data.frame(x=x,y=rnorm(100))
and I want to label values that are sorted (descending) in the 80th percentile for each value of x (1:10). I can get the quantiles and order the data, without issue like this:
df <- ddply(df, .(x), subset, y > quantile(y,0.8))
df <- df[with(df, order(x,-y)),]
Now, how could I get ddply to add a column of labels (1,2,3,...n) in a new column of the data.frame for each sorted subset? I can do this now with a for loop by counting nrow(df["x"]), but that seems to lack any sense of eloquence.
Note: This question is a build up from and related to: http://stackoverflow.com/questions/3370621/creating-multiple-subsets-all-in-one-data-frame-possibly-with-ddply