[R] Holiday in Canada

arun smartpink111 at yahoo.com
Sat Jan 5 19:41:55 CET 2013


HI Fares,


date1<- seq.Date(as.Date("1jan2003",format="%d%b%Y"),as.Date("1jan2013",format="%d%b%Y"),by="day")
   length(date1)
#[1] 3654F
 set.seed(51)
 donation<-sample(1000:3000000,3654,replace=FALSE)
 dat1<-data.frame(date1,donation)
 library(chron)
nrow(dat1[is.weekend(dat1[,1]),])
#[1] 1044
set.seed(15)
 dat1[,2][is.weekend(dat1[,1])]<-sample(1:500,1044,replace=TRUE)
 dat2<-dat1
library(timeDate) #Not sure these are the only Canadian holidays, but the package says so
listHolidays("CA")
#[1] "CACanadaDay"              "CACivicProvincialHoliday"
#[3] "CALabourDay"              "CAThanksgivingDay"       
#[5] "CAVictoriaDay"     

dat1$HolidayCA<-NA
HolidayCA<-sort(as.Date(c(CACanadaDay(2003:2012),CACivicProvincialHoliday(2003:2012),CALabourDay(2003:2012),CAThanksgivingDay(2003:2012),CAVictoriaDay(2003:2012))))
 dat1[,3][dat1[,1]%in%HolidayCA]<-"HolidayCA"
dat1[180:185,]
#         date1 donation HolidayCA
#180 2003-06-29      358      <NA>
#181 2003-06-30  2895407      <NA>
#182 2003-07-01  1619049 HolidayCA
#183 2003-07-02  1391688      <NA>
#184 2003-07-03   422074      <NA>
#185 2003-07-04  1481923      <NA>

 #or    

 HolidaysCA<-data.frame(date1=as.Date(c(CACanadaDay(2003:2012),CACivicProvincialHoliday(2003:2012),CALabourDay(2003:2012),CAThanksgivingDay(2003:2012),CAVictoriaDay(2003:2012))),Holiday=rep(c("CACanadaDAy","CACivicProvincialHoliday","CALabourDay","CAThanksgivingDay","CAVictoriaDAy"),each=10))
 HolidaysCA<-HolidaysCA[order(HolidaysCA[,1]),]
row.names(HolidaysCA)<-1:nrow(HolidaysCA)
library(plyr)
 res<-join(dat2,HolidaysCA,by="date1")
res[180:185,]
#         date1 donation     Holiday
#180 2003-06-29      358        <NA>
#181 2003-06-30  2895407        <NA>
#182 2003-07-01  1619049 CACanadaDAy
#183 2003-07-02  1391688        <NA>
#184 2003-07-03   422074        <NA>
#185 2003-07-04  1481923        <NA>


A.K.
    






________________________________
From: Fares Said <frespider at hotmail.com>
To: arun <smartpink111 at yahoo.com> 
Sent: Saturday, January 5, 2013 8:23 AM
Subject: Holiday in Canada



Hi A.k,

I need one more favor from you,

How can I determine the canadian holidays the the data I created using your code to generate.

Thanks




More information about the R-help mailing list