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))
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:
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.