Skip to content

Instantly share code, notes, and snippets.

@gcamilo
Created November 22, 2016 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcamilo/1175b4e01da21e94dac80e74dd3aeade to your computer and use it in GitHub Desktop.
Save gcamilo/1175b4e01da21e94dac80e74dd3aeade to your computer and use it in GitHub Desktop.
annual_data <- data.table(year=1991:2000,ret=rnorm(200),group=1:20)
yearly_deciles <- annual_data[,data.table(t(quantile(ret,probs=seq(from=.1, to = 1, length.out=10)))),by=year]
find_decile <- function(thisReturn,year,decile_table){
for(i in 2:ncol(decile_table)){
if(thisReturn<decile_table[year==year,i,with=F])
return(i-1)
}
}
annual_data[,decile:=find_decile(ret,year,yearly_deciles),by=c("group","year")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment