I have some data that looks like the following. It is grouped by variable \"Year\" and I want to extract the percentiles of each observation of Score, with respect to t
I found a method, but it requires a loop.
group.pctiles <- function(group.var, comparable) {
unique.vals <- unique(group.var)
pctiles <- vector(length = length(group.var))
for (i in 1:length(unique.vals)) {
slice <- which(group.var == unique.vals[i])
F <- ecdf(comparable[slice])
group.pctiles <- F(comparable[slice])
pctiles[slice] <- group.pctiles
}
return(pctiles)
}
group.var is the variable that groups the data. In my example in my question, it is Year. comparable contains the values we want to find the percentiles for. In my question, comparable would be Score.
For the following data, I get the result below:
Year,School,Fees
2000,10,1000
2008,1,1050
2008,4,2000
2000,3,1700
2000,1,2000
> group.pctiles(dat, dat$Year, dat$Fees)
[1] 0.3333333 0.5000000 1.0000000 0.6666667 1.0000000
Then, I can cbind these percentiles back into the original data.frame for analysis, reporting, etc.
Anyone have a solution that doesn't require a loop?