Created
November 22, 2016 16:09
-
-
Save gcamilo/1175b4e01da21e94dac80e74dd3aeade to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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