[R] Function to assign quantiles?
Talbot Katz
topkatz at msn.com
Thu Feb 15 23:50:16 CET 2007
Hi.
If I call the quantiles function as follows:
qvec = quantiles(dvals,probs=seq(0,1,0.1))
the results will return a vector something like the following example:
0% 10% 20% 30% 40% 50% 60% 70% 80% 90%
100%
56.0 137.3 238.4 317.9 495.8 568.5 807.4 1207.7 1713.0
2951.1 8703.0
Now I want to assign the deciles, 1 - 10, to each observation, so, in the
above example, if dvals[322] = 256, I want to assign qvals[322] = 3, and if
dvals[7216] = 1083, I want qvals[7216] = 7, etc. I would think there would
be a function, or some very quick code to do that, but I couldn't find it.
Here's what I have now. It works, but I figure there must be a better way:
asdc <- function(q){max(1,which(qvec<q))}
qvals = apply(matrix(dvals,nrow=1),2,asdc)
Any suggestions? Thanks!
-- TMK --
212-460-5430 home
917-656-5351 cell
More information about the R-help
mailing list