Reputation: 1258
I need to assign deciles to a distribution but in the form of integers. Like 1 should correspond to the first decile, 2 to the second, 3 to the third... at the moment I'm using the following code that does not do exactly what I am looking for.
cut(x, breaks = quantile(data, probs = seq(0, 1, 0.1)))
So, instead of this type of result:
(0,100] (100,200]
I would like to have something like:
1 90
2 130
2 150
3 230
Does anyone already have some code for this?
Upvotes: 1
Views: 1210
Reputation: 13807
If you're working with a data.frame.
, you can do this:
df$decile <- cut(df$x, breaks= quantile(df$x, probs= seq(0, 1, by= 0.1)),
include.lowest= TRUE,
labels= c(1:10))
in case you want to use a data.table
approach
library(data.table)
setDT(df)[, decile := cut(x, breaks= quantile(x, probs= seq(0, 1, by= 0.1)),
include.lowest= TRUE,
labels= c(1:10)) ]
Upvotes: 1
Reputation: 9923
You can use the labels
argument in cut
x2 <- cut(x, breaks = quantile(x, probs = seq(0, 1, 0.1)),
labels = 1:10, include.lowest = TRUE)
This will return a factor. Run
as.numeric(as.character(x2))
to convert this to an integer (yes, I know in this particular case the as.character is redundant).
Upvotes: 3