dear All, i'm trying to calculate the number of Mondays, Tuesdays, etc that each month within a date range has. I have time series data that spans 60 months and i want to calculate the number of Mondays, Tuesdays, Wed, etc of each month. (I want to control for weekly seasonality but my data is monthly). Is there an easy way to to this in R? or is there a package i could use? i did some quick search in the help files and R sites but could not find any answers. i appreciate any hint you could give, thanks. Carlos
?chron() in particular day.of.week -Roy On Jan 15, 2009, at 11:28 AM, Carlos Hernandez wrote:> dear All, > i'm trying to calculate the number of Mondays, Tuesdays, etc that > each month within a date range has. I have time series data that > spans 60 months and i want to calculate the number of Mondays, > Tuesdays, Wed, etc of each month. (I want to control for weekly > seasonality but my data is monthly). > > Is there an easy way to to this in R? or is there a package i could > use? i did some quick search in the help files and R sites but could > not find any answers. > > i appreciate any hint you could give, > > thanks. > > Carlos > > ______________________________________________ > 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.********************** "The contents of this message do not reflect any position of the U.S. Government or NOAA." ********************** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center 1352 Lighthouse Avenue Pacific Grove, CA 93950-2097 e-mail: Roy.Mendelssohn at noaa.gov (Note new e-mail address) voice: (831)-648-9029 fax: (831)-648-8440 www: http://www.pfeg.noaa.gov/ "Old age and treachery will overcome youth and skill." "From those who have been given much, much will be expected"
Try this:> library(zoo) # as.yearmon > dd <- seq(as.Date("2000-01-01"), as.Date("2004-12-31"), "day") > dow <- as.numeric(format(dd, "%w")) > ym <- as.yearmon(dd) > tab <- do.call(rbind, tapply(dow, ym, table)) > rownames(tab) <- format(as.yearmon(as.numeric(rownames(tab)))) > head(tab)0 1 2 3 4 5 6 Jan 2000 5 5 4 4 4 4 5 Feb 2000 4 4 5 4 4 4 4 Mar 2000 4 4 4 5 5 5 4 Apr 2000 5 4 4 4 4 4 5 May 2000 4 5 5 5 4 4 4 Jun 2000 4 4 4 4 5 5 4 On Thu, Jan 15, 2009 at 2:28 PM, Carlos Hernandez <carlos.uni2 at gmail.com> wrote:> dear All, > i'm trying to calculate the number of Mondays, Tuesdays, etc that each month > within a date range has. I have time series data that spans 60 months and i > want to calculate the number of Mondays, Tuesdays, Wed, etc of each month. > (I want to control for weekly seasonality but my data is monthly). > > Is there an easy way to to this in R? or is there a package i could use? i > did some quick search in the help files and R sites but could not find any > answers. > > i appreciate any hint you could give, > > thanks. > > Carlos > > ______________________________________________ > 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. >
On Jan 15, 2009, at 2:28 PM, Carlos Hernandez wrote:> dear All, > i'm trying to calculate the number of Mondays, Tuesdays, etc that > each month within a date range has. I have time series data that > spans 60 months and i want to calculate the number of Mondays, > Tuesdays, Wed, etc of each month. (I want to control for weekly > seasonality but my data is monthly). > > Is there an easy way to to this in R? or is there a package i could > use? i did some quick search in the help files and R sites but could > not find any answers.The chron package has a wide assortment of functions of that sort. Looks like Mondays would be 1 > day.of.week(1,15,2009) # a Thursday [1] 4 > day.of.week(1,18,2009) #Saturday [1] 0 > day.of.week(1,17,2009) #Sunday [1] 6 -- David Winsemius
Carlos Hernandez wrote:> dear All, > i'm trying to calculate the number of Mondays, Tuesdays, etc that each > month within a date range has. I have time series data that spans 60 > months and i want to calculate the number of Mondays, Tuesdays, Wed, etc > of each month. (I want to control for weekly seasonality but my data is > monthly). > > Is there an easy way to to this in R? or is there a package i could use? > i did some quick search in the help files and R sites but could not find > any answers. > > i appreciate any hint you could give,This is where POSIXlt objects are useful:> unlist(unclass(as.POSIXlt(ISOdate(1959,3,11))))sec min hour mday mon year wday yday isdst 0 0 12 11 2 59 3 69 0 Which means that I was born on a Wednesday (wday==3) in March (mon==2) (some of the fields count from 0 and others, like mday, from 1; presumably some UNIX vendor back in the Stone Age got their implementation turned into a standard...). This allows you to do stuff like:> dd <- seq(Sys.Date(),as.Date("2009-3-11"),1) > dd <- as.POSIXlt(dd) > with(dd, table(mon,wday))wday mon 0 1 2 3 4 5 6 0 2 2 2 2 3 3 3 1 4 4 4 4 4 4 4 2 2 2 2 2 1 1 1 which I think is pretty much what you were looking for.> thanks. > > Carlos > > ______________________________________________ > 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.-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907