R: Assigning variable to quintile on monthly basis -


i trying in r indicate in quintile value of variable every month of data frame in case based on volatility. each month want know each stock if in volatile quintile of if in 1 of others.

so far have come following function (see below). unfortunately, function works in cases , gives following error:

error in cut.default(df$volatility, unique(breaks), label = false, na.rm =true):    invalid number of intervals 

could give me advice on how improve code works properly.

it's relatively urgent. many thanks!

quintilesvolbymonth <- function(x){   months<-as.vector(unique(x$date))     dfx<-data.frame()   for(n in seq(1,length(months))){     num<-5     print(paste("appending month",months[n],sep=""))     df<-subset(x,date==months[n])     breaks<-quantile(df$volatility,probs=seq(0,1, 1/num),na.rm=true)     df$volquintile <- cut(df$volatility,unique(breaks),                         label=false, na.rm=true)     dfx<-rbind(dfx,df)   }   return(dfx) } frame.quintile <- quintilesvolbymonth(x)    

example of data: last column trying get. data here example , not actual results.

> date <- c("01/10/2011","01/10/2012","01/10/2010","01/08/2010","01/10/2011","01/12/2011","01/09/2011","01/10/2011","01/09/2012","01/08/2012","01/02/2010","01/01/2011","01/09/2010","01/06/2010","01/07/2010","01/01/2012","01/01/2012","01/11/2011","01/09/2011","01/10/2011") > name<-c("hoek's machine dead - delist.","world scope (cadb test stock)","brill (kon.)",   "bbl dead - 30/06/465", "genk logistics","groenijk.ylcbn. dead - delist.31/05/479", "noord-eur.houth.","palthe dead - 4/2/475","generale banque dead - del. 30/12/490","stork dead - takeover 905099","louvain-la-neuve","ventos dead - 06/06/384","braine-le-comte susp 14/02/460","vilenzo dead - 25/11/370","econosto kon. dead - 07/07/374","electrorail dead - delist 21/02/387","blystein fl.1384","obourg (ciments)","brugefi dead - 31/07/475","gib new") > volatility<-c(0.3383, 0.084,  0.046,  0.0945, 0.0465, 0.2008, 0.1361, 0.2183, 0.1032, 0.1083, 0.0494, 0.0538, 0.0357, 0.037,  0.0386, 0.073,  0.073,  0.0393, 0.0687, 0.3308) > volquintile<-c(4,1,1,2,2,3,2,3,4,2,3,2,4,1,2,1,1,2,3,4) >    > x<-data.frame(date,name,volatility, volquintile) > x          date                                    name volatility volquintile 1  01/10/2011           hoek's machine dead - delist.     0.3383           4 2  01/10/2012           world scope (cadb test stock)     0.0840           1 3  01/10/2010                            brill (kon.)     0.0460           1 4  01/08/2010                    bbl dead - 30/06/465     0.0945           2 5  01/10/2011                          genk logistics     0.0465           2 6  01/12/2011 groenijk.ylcbn. dead - delist.31/05/479     0.2008           3 7  01/09/2011                        noord-eur.houth.     0.1361           2 8  01/10/2011                   palthe dead - 4/2/475     0.2183           3 9  01/09/2012   generale banque dead - del. 30/12/490     0.1032           4 10 01/08/2012            stork dead - takeover 905099     0.1083           2 11 01/02/2010                        louvain-la-neuve     0.0494           3 12 01/01/2011                 ventos dead - 06/06/384     0.0538           2 13 01/09/2010          braine-le-comte susp 14/02/460     0.0357           4 14 01/06/2010                vilenzo dead - 25/11/370     0.0370           1 15 01/07/2010          econosto kon. dead - 07/07/374     0.0386           2 16 01/01/2012     electrorail dead - delist 21/02/387     0.0730           1 17 01/01/2012                        blystein fl.1384     0.0730           1 18 01/11/2011                        obourg (ciments)     0.0393           2 19 01/09/2011                brugefi dead - 31/07/475     0.0687           3 20 01/10/2011                                 gib new     0.3308           4 

does work you?

library(plyr) vol1<-ddply(mydata,.(date), transform, max.name=name[which.max(quantile(volatility))])              date                                    name volatility                                max.name     1  01/01/2011                 ventos dead - 06/06/384     0.0538                 ventos dead - 06/06/384     2  01/01/2012     electrorail dead - delist 21/02/387     0.0730     electrorail dead - delist 21/02/387     3  01/01/2012                        blystein fl.1384     0.0730     electrorail dead - delist 21/02/387     4  01/02/2010                        louvain-la-neuve     0.0494                        louvain-la-neuve     5  01/06/2010                vilenzo dead - 25/11/370     0.0370                vilenzo dead - 25/11/370     6  01/07/2010          econosto kon. dead - 07/07/374     0.0386          econosto kon. dead - 07/07/374     7  01/08/2010                    bbl dead - 30/06/465     0.0945                    bbl dead - 30/06/465     8  01/08/2012            stork dead - takeover 905099     0.1083            stork dead - takeover 905099     9  01/09/2010          braine-le-comte susp 14/02/460     0.0357          braine-le-comte susp 14/02/460     10 01/09/2011                        noord-eur.houth.     0.1361                                    <na>     11 01/09/2011                brugefi dead - 31/07/475     0.0687                                    <na>     12 01/09/2012   generale banque dead - del. 30/12/490     0.1032   generale banque dead - del. 30/12/490     13 01/10/2010                            brill (kon.)     0.0460                            brill (kon.)     14 01/10/2011           hoek's machine dead - delist.     0.3383                                    <na>     15 01/10/2011                          genk logistics     0.0465                                    <na>     16 01/10/2011                   palthe dead - 4/2/475     0.2183                                    <na>     17 01/10/2011                                 gib new     0.3308                                    <na>     18 01/10/2012           world scope (cadb test stock)     0.0840           world scope (cadb test stock)     19 01/11/2011                        obourg (ciments)     0.0393                        obourg (ciments)     20 01/12/2011 groenijk.ylcbn. dead - delist.31/05/479     0.2008 groenijk.ylcbn. dead - delist.31/05/479 

updated solution:

library(plyr)          vol2<-ddply(x,.(date), transform,quantile=ifelse(volatility<quantile(volatility,p=0.25),1, ifelse(((volatility>quantile(volatility,p=0.25))& (volatility<quantile(volatility,p=0.5))),2,ifelse(((volatility>quantile(volatility,p=0.5))& volatility<quantile(volatility,p=0.75)),3,4))))         date                                    name volatility   quantile 1  01/01/2011                 ventos dead - 06/06/384     0.0538        4 2  01/01/2012     electrorail dead - delist 21/02/387     0.0730        4 3  01/01/2012                        blystein fl.1384     0.0730        4 4  01/02/2010                        louvain-la-neuve     0.0494        4 5  01/06/2010                vilenzo dead - 25/11/370     0.0370        4 6  01/07/2010          econosto kon. dead - 07/07/374     0.0386        4 7  01/08/2010                    bbl dead - 30/06/465     0.0945        4 8  01/08/2012            stork dead - takeover 905099     0.1083        4 9  01/09/2010          braine-le-comte susp 14/02/460     0.0357        4 10 01/09/2011                        noord-eur.houth.     0.1361        4 11 01/09/2011                brugefi dead - 31/07/475     0.0687        1 12 01/09/2012   generale banque dead - del. 30/12/490     0.1032        4 13 01/10/2010                            brill (kon.)     0.0460        4 14 01/10/2011           hoek's machine dead - delist.     0.3383        4 15 01/10/2011                          genk logistics     0.0465        1 16 01/10/2011                   palthe dead - 4/2/475     0.2183        2 17 01/10/2011                                 gib new     0.3308        3 18 01/10/2012           world scope (cadb test stock)     0.0840        4 19 01/11/2011                        obourg (ciments)     0.0393        4 20 01/12/2011 groenijk.ylcbn. dead - delist.31/05/479     0.2008        4 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -