R: How to create a Quartile Column within Groups

1.6k views Asked by At

I have managed to create the column "qaurtile" with the following code, but I'd also like to create a column called "quartile_team" that shows the quartiles within each team. I can't figure out how to do this.

Help is appreciated,

Paul

# generate dataset
teams <- c(rep("East", 6), rep("West", 8), rep("North", 7), rep("South", 9))
time_spent <- rnorm(30)
dataset <- as.data.frame(cbind(teams, time_spent))
dataset$time_spent <- as.numeric(dataset$time_spent)

# create quartile column
 dataset <- within(dataset,
                    quartile <- cut(x = time_spent,
                                    breaks = quantile(time_spent, probs = seq(0, 1, 0.25)),
                                    labels = FALSE,
                                    include.lowest = TRUE))
1

There are 1 answers

0
alexizydorczyk On BEST ANSWER

There's far better way to do this but a quick and dirty solution would probably use plyr. I'll use your function for calculating quartiles within:

library(plyr)


ddply(dataset, "teams", function(team){

  team_quartile <- cut(x = team$time_spent, breaks = quantile(team$time_spent, probs = seq(0, 1, 0.25)),
                       labels = FALSE,
                       include.lowest = TRUE)

  data.frame(team, team_quartile)
})

Basically, you want to split the data frame up by the team and then perform the calculation on each subset of the data frame. You could use tapply for this as well.