[R] calculating quintile values of numeric data?

David L Carlson dc@r|@on @end|ng |rom t@mu@edu
Wed Jan 23 16:19:58 CET 2019


Also quantile() and cut(). The only tricky part is making sure the minimum and maximum values are included.

> set.seed(42)
> x <- rnorm(100, 25, 3)
> bks <- quantile(x, prob=c(0, .2, .4, .6, .8, 1))
> y <- cut(x, breaks=bks, labels=1:5, include.lowest=TRUE)
> table(y)
y
 1  2  3  4  5 
20 20 20 20 20
> z <- findInterval(x, bks, all.inside=TRUE)
> table(z)
z
 1  2  3  4  5 
20 20 20 20 20

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352


-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Jeff Newmiller
Sent: Wednesday, January 23, 2019 1:00 AM
To: r-help using r-project.org; Kelly Thompson <kt1572757 using gmail.com>
Subject: Re: [R] calculating quintile values of numeric data?

?range
?findInterval

On January 22, 2019 6:54:14 PM PST, Kelly Thompson <kt1572757 using gmail.com> wrote:
>I’d like to take numeric data, and calculate numeric “quintiles” with
>integer values in from 1 – 5 , with values in the lowest 20% of values
>having a value of 1, the >20 - <= 40% of values having a value of 2,
>the >40% - <=60% of values having a value of 3, etc.
>
>How can I use quantcut, or another function, to do this?
>
>
>Thanks!
>
>
>Ex.
>
>x <- c(1:10)
>
>I want:
>myquintilefunction (x, q=5, na.rm=T) to return a vector with values:
>1,1,2,2,3,3,4,4,5,5
>
>Thanks!
>
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.

-- 
Sent from my phone. Please excuse my brevity.

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


More information about the R-help mailing list