[R] number of Mondays

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jan 15 21:23:34 CET 2009


Or for those not allergic to reading help, see ?weekdays .

Just how hard do you have to work to miss that?  E.g. ??day works.

On Thu, 15 Jan 2009, Peter Dalgaard wrote:

> 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
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list