[R] Function to assign quantiles?
Frank E Harrell Jr
f.harrell at vanderbilt.edu
Fri Feb 16 01:04:03 CET 2007
Marc Schwartz wrote:
> On Thu, 2007-02-15 at 17:50 -0500, Talbot Katz wrote:
>> 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!
>
> Take a look at ?cut and use the output of quantile() to define the
> 'breaks' argument:
>
> x <- rnorm(100)
>
>> cut(x, breaks = quantile(x, probs = seq(0, 1, 0.1)),
> include.lowest = TRUE, labels = 1:10)
> [1] 2 1 9 10 9 3 1 7 5 2 4 1 10 8 4 10 9 9 5 3 5 8
> [23] 6 9 7 1 10 1 10 9 3 3 9 3 5 5 6 4 8 10 6 2 2 8
> [45] 6 3 6 9 1 4 7 10 8 7 5 3 1 10 2 1 7 8 8 2 8 8
> [67] 3 10 4 6 5 1 4 6 4 4 5 9 9 7 2 8 2 5 1 3 7 6
> [89] 4 6 4 6 2 7 2 10 7 3 5 7
> Levels: 1 2 3 4 5 6 7 8 9 10
>
>
> HTH,
>
> Marc Schwartz
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Or see the cut2 function in the Hmisc package
More information about the R-help
mailing list