[R] Quintile by month vector
Pierre Lapointe
pierrelap at gmail.com
Tue Dec 5 16:43:56 CET 2006
Hello,
Question: How do I create a QUINTILE BY MONTH vector
#Here's my dataset
set.seed(1)
dates <- c(rep("2006-01-01",6),rep("2007-02-01",6))
value <-runif(12)
DF <-data.frame(cbind(dates,value),stringsAsFactors=FALSE)
DF$dates <-as.Date(DF$dates,format="%Y-%m-%d")
DF$value <-as.numeric(DF$value)
#Here's how I calculate quintiles for whole dataset
library(Hmisc)
as.numeric(cut2(DF$value, g=5))
# [1] 2 3 3 5 1 4 5 4 3 1 2 1
Now, I need to calculate quintiles by month and year
For example the quintiles for observations in January 2006 would be
as.numeric(cut2(
subset(DF,as.numeric(format(DF$dates, "%m"))==1&
as.numeric(format(DF$dates, "%Y"))==2006,select="value",drop=TRUE)
, g=5))
#[1] 1 2 3 5 1 4
In other words, I'm looking for a vector that would give the January
2006 quintile if the observation is in January and February 2007
quintile if in February 2007.
More information about the R-help
mailing list