Find quantiles in R

This code returns a vector of n (‘n’) quantiles for a numerical vector (‘variable’), e.g. quant(x, 10).

quant <- function(variable, n) {
    x <- data.frame(id = 1:length(variable), variable = variable)
    x <- x[order(x$variable),]
    cases <- length(variable[!is.na(variable)])
    sizes <- floor(cases/n)
    breaks <- rep(sizes, n)
    left <- cases - (sizes * n)
    if(left > 0) {
        breaks[1:left] <- rep(sizes + 1)
    }
    vec <- NULL
    for (u in 1:n) {
        vec <- c(vec, rep(u, breaks[u]))
    }
    vec <- c(vec, rep(NA, length(variable) - cases))
    return(vec[order(x$id)])
}

Edit – better method (borrowing heavily from dplyr::ntile):

ntile_ <- function(x, n) {
    b <- x[!is.na(x)]
    q <- floor((n * (rank(b, ties.method = "first") - 1)/length(b)) + 1)
    d <- rep(NA, length(x))
    d[!is.na(x)] <- q
    return(d)
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s