[R] Generating weekdays only

David Winsemius dwinsemius at comcast.net
Mon Oct 4 20:51:28 CEST 2010


On Oct 4, 2010, at 2:07 PM, Ron Michael wrote:

> Dear all, can anyone please tell me how to generate a sequence of  
> days continuously, however without considering weekends i.e.  
> Saturday and Sunday? I am aware of following code:
>
>> seq(as.Date("2010-01-01"), as.Date("2010-02-01"), by="1 day")
>  [1] "2010-01-01" "2010-01-02" "2010-01-03" "2010-01-04"  
> "2010-01-05" "2010-01-06" "2010-01-07" "2010-01-08"
>  [9] "2010-01-09" "2010-01-10" "2010-01-11" "2010-01-12"  
> "2010-01-13" "2010-01-14" "2010-01-15" "2010-01-16"
> [17] "2010-01-17" "2010-01-18" "2010-01-19" "2010-01-20"  
> "2010-01-21" "2010-01-22" "2010-01-23" "2010-01-24"
> [25] "2010-01-25" "2010-01-26" "2010-01-27" "2010-01-28"  
> "2010-01-29" "2010-01-30" "2010-01-31" "2010-02-01"
>

??weekdays
?weekdays  # generates a character vector that can be tested against  
non Sat/Sun values

(as.Date("2010-01-01")+0:31)[ !weekdays((as.Date("2010-01-01")+0:31))  
%in% c("Saturday" , "Sunday") ]

> However this generates all days.
> Thanks for your time.


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list