I want to separate by data into quantiles, same as in this great question
The problem is that I want to do it every day in a time series:
set.seed(123)
temp.all <- data.frame(date = c(rep(Sys.Date() - 1, 12), rep(Sys.Date(), 12)),
name=letters[c(1:12, 1:12)], value=rnorm(24))
At the moment, I'm solving with a for-loop:
library(dplyr)
for (d in unique(temp.all$date)) {
temp = filter(temp.all, date == d)
temp$quartile <- with(temp, factor(
findInterval( val, c(-Inf,
quantile(val, probs=c(0.25, .5, .75)), Inf) , na.rm=TRUE),
labels=c("Q1","Q2","Q3","Q4")
))
# ...and doing rbind on 'temp' to reconstruct temp.all with quartiles
}
Any ideas on how to avoid the dreaded for-loop? Is there maybe a group_by
way of doing this?