Computing Quantiles for a column in R to subset
r
Solution
You can do this using `cut` and `quantile`.
# some data
df <- data.frame(name=letters , am.spent = rnorm(26))
# divide df$am.spent
df$qnt<- cut(df$am.spent , breaks=quantile(df$am.spent),
labels=1:4, include.lowest=TRUE)
# check ranges
tapply(df$am.spent , df$qnt , range)
First get the `quantile` quantile(df$am.spent)
# 0% 25% 50% 75% 100%
#-3.5888426 -0.6879445 -0.1461107 0.5835165 1.2030989
Then use `cut` to divide df$am.spent at specified cutpoints - we cut at the values of the quantiles. This is specified with the `breaks`argument
Problem
I have a data set with the following structure: ``` Name=c("a","b","c") Amount_Spent=c(386407,213918,212006) ``` What I am trying to do is compute which quartile the `Amount_Spent` falls under for each name and assign the value to a new variable (column) `Quantiles`. I am not able to use any of the apply functions to get this result, can someone help please? Thanks in advance, Raoul