[R] Find out the day of week for a chron object?
Ajay Shah
ajayshah at mayin.org
Wed Mar 3 19:33:59 CET 2004
On Wed, Mar 03, 2004 at 06:58:02AM -0500, Gabor Grothendieck wrote:
>
> Here are three different ways (using x as defined in your
> post):
>
> with( month.day.year(x), day.of.week(month,day,year) )
>
> do.call( "day.of.week", month.day.year(x) )
>
> as.numeric(x-3)%%7 # uses fact that chron(3) is Sunday
Thanks! Using this, I wrote --
library(chron);
prevFriday <- function(x) {
repeat {
x <- x - 1;
if (5 == with(month.day.year(x), day.of.week(month,day,year))) break;
}
return(x);
}
x = dates("12-02-04", format="d-m-y")
print(prevFriday(x))
and it works. :-)
Could someone give me a glimmer into HOW and WHY that expression
with(month.day.year(x), day.of.week(month,day,year))
works? :-)
--
Ajay Shah Consultant
ajayshah at mayin.org Department of Economic Affairs
http://www.mayin.org/ajayshah Ministry of Finance, New Delhi
More information about the R-help
mailing list