Skip to content
Prev 327717 / 398502 Next

Add a column to a data frame with value based on the percentile of the row

Hello,

Combine quantile() with findInterval(). Something like the following.


# sample data
x <- rnorm(100)

val <- c("Bottom 50", "20 to 50", "5 to 20", "Top 5%")
qq <- quantile(x, probs = c(0, 0.50, 0.70, 0.95, 1))

idx <- findInterval(x, qq)
val[idx]


Hope this helps,

Rui Barradas

Em 31-07-2013 10:37, Dark escreveu: