Hi, I am trying in R to indicate in which quintile a value of a variable is for every month of my data frame in this case based on volatility. For each month I want to know for each stock if it is in the most volatile quintile of if it is in one of the others. So far I have come up with the following function (see below). Unfortunately, the function only works in some cases and often gives the following error: Error in cut.default(df$VOLATILITY, unique(breaks), label = FALSE, na.rm =TRUE): invalid number of intervals Could you give me some advice on how to improve this code so that it 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: (I have 30 years of monthly data for over 1000 stocks) Variable 'VolQuintile' is what I am trying to get. DATE STOCK VOLATILITY VolQuintile 01/01/1990 STOCK 1 0.3 1 01/01/1991 STOCK 2 0.15 4 01/05/1992 STOCK 3 0.1 3 . [[alternative HTML version deleted]]
David Winsemius
2013-Apr-06 01:10 UTC
[R] Cross posted Re: Creating quintiles on monthly basis
Also posted at StackOverflow but now has an example dataset there. Cedric: You are requested not to cross-post to Rhelp. There are many other requests in the Posting Guide as well including a request that you not post in HTML. -- David. On Apr 5, 2013, at 5:20 PM, Cedric Suykens wrote:> Hi, > > I am trying in R to indicate in which quintile a value of a variable is for > every month of my data frame in this case based on volatility. For each > month I want to know for each stock if it is in the most volatile quintile > of if it is in one of the others. > > So far I have come up with the following function (see below). > Unfortunately, the function only works in some cases and often gives the > following error: > > Error in cut.default(df$VOLATILITY, unique(breaks), label = FALSE, na.rm > =TRUE): > > invalid number of intervals > > Could you give me some advice on how to improve this code so that it 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: (I have 30 years of monthly data for over 1000 stocks) > Variable 'VolQuintile' is what I am trying to get. > > > > DATE STOCK VOLATILITY VolQuintile > > 01/01/1990 STOCK 1 0.3 > 1 > > 01/01/1991 STOCK 2 0.15 > 4 > > 01/05/1992 STOCK 3 0.1 > 3 > > . > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA