I'd like to use ntile
from package dplyr
to generate a vector of quantiles. The problem occurs when I have a low number of groups to divide my data into.
For example, if I have a vector of -1
and 1
, the value -1
should be in quantile 1 and value 1
should be in quantile 2:
library(dplyr)
index2 <- rep(c(-1,1,-1),each=4)
#[1] -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1
However, using ntile
, the last two data points are in the wrong quantile (2 instead of 1)
ntile(index2,2)
# [1] 1 1 1 1 2 2 2 2 1 1 2 2
Here's the result I would expect for the index2 quantiles:
# 1 1 1 1 2 2 2 2 1 1 1 1
I have the same problem with n=3
. The results are not as expected.
index3 <- rep(c(-1,1,-2,-2),each=3)
#[1] -1 -1 -1 1 1 1 -2 -2 -2 -2 -2 -2
ntile(index3,3)
#[1] 2 2 3 3 3 3 1 1 1 1 2 2
Here's the result I would expect for the index3 quantiles:
# 2 2 2 3 3 3 1 1 1 1 1 1
I'm also open to a cut
and quantile()
solution.