I know that this is correct: library(chron) x = dates("01-03-04", format="d-m-y", out.format="day mon year") print(x) It gives me the string "01 Mar 2004" which is correct. I also know that I can say: print(day.of.week(3,1,2004)) in which case he says 1, for today is monday. My question is: How do I combine these two!? :-) I have a data file which is being parsed nicely and read in using the chron() function. I need to identify fridays and treat them differently. So I need to run the day.of.week function. But day.of.week() doesn't eat a chron object, he insists he wants m,d,y. This seems quite odd. Any idea what I can do? Thanks, -ans. -- Ajay Shah Consultant ajayshah at mayin.org Department of Economic Affairs http://www.mayin.org/ajayshah Ministry of Finance, New Delhi
On Mon, Mar 01, 2004 at 10:18:33PM +0530, Ajay Shah wrote:> I know that this is correct: > > library(chron) > x = dates("01-03-04", format="d-m-y", out.format="day mon year") > print(x) > > It gives me the string "01 Mar 2004" which is correct. > > > I also know that I can say: > > print(day.of.week(3,1,2004)) > > in which case he says 1, for today is monday. > > > My question is: How do I combine these two!? :-) I have a data file > which is being parsed nicely and read in using the chron() function. I > need to identify fridays and treat them differently. So I need to run > the day.of.week function. But day.of.week() doesn't eat a chron > object, he insists he wants m,d,y. This seems quite odd. Any idea what > I can do?Chron and date are older packages, you may want to use the more recent (and very powerful) DateTimeClasses> parsedDate <- strptime("01-03-04", "%d-%d-%y") > format(parsedDate)[1] "2004-01-03"> class(parsedDate)[1] "POSIXt" "POSIXlt"> weekdays(parsedDate)[1] "Saturday" Start with help(DateTimeClasses), if you ever used the C functions strptime and strftime it shouldn't be too foreign. And do look at the mailing list archives (and/or Google), as questions get answered on this quite often. Dirk -- The relationship between the computed price and reality is as yet unknown. -- From the pac(8) manual page
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 --- Date: Mon, 1 Mar 2004 22:18:33 +0530 From: Ajay Shah <ajayshah at mayin.org> To: r-help <r-help at stat.math.ethz.ch> Subject: [R] Find out the day of week for a chron object? I know that this is correct: library(chron) x = dates("01-03-04", format="d-m-y", out.format="day mon year") print(x) It gives me the string "01 Mar 2004" which is correct. I also know that I can say: print(day.of.week(3,1,2004)) in which case he says 1, for today is monday. My question is: How do I combine these two!? :-) I have a data file which is being parsed nicely and read in using the chron() function. I need to identify fridays and treat them differently. So I need to run the day.of.week function. But day.of.week() doesn't eat a chron object, he insists he wants m,d,y. This seems quite odd. Any idea what I can do? Thanks, -ans. -- Ajay Shah Consultant ajayshah at mayin.org Department of Economic Affairs http://www.mayin.org/ajayshah Ministry of Finance, New Delhi
The specific code given below is correct but, in general, using POSIXt is not a good idea since it gives rise to subtle problems with time zones. You won't run into problems just converting character data to POSIXlt, as the code below does, but most people who use POSIXlt also use POSIXct and that is where you run into problems. The archives are full of such subtle time zone problems. I have even found and reported subtle time zone problems in widely used R packages. chron is more suitable for typical statistics problems that don't require time zones. What POSIXt is good for is file stamps and other operating system related time problems. --- Date: Tue, 2 Mar 2004 23:01:51 -0600 From: Dirk Eddelbuettel <edd at debian.org> To: Ajay Shah <ajayshah at mayin.org> Cc: r-help <r-help at stat.math.ethz.ch> Subject: Re: [R] Find out the day of week for a chron object? On Mon, Mar 01, 2004 at 10:18:33PM +0530, Ajay Shah wrote:> I know that this is correct: > > library(chron) > x = dates("01-03-04", format="d-m-y", out.format="day mon year") > print(x) > > It gives me the string "01 Mar 2004" which is correct. > > > I also know that I can say: > > print(day.of.week(3,1,2004)) > > in which case he says 1, for today is monday. > > > My question is: How do I combine these two!? :-) I have a data file > which is being parsed nicely and read in using the chron() function. I > need to identify fridays and treat them differently. So I need to run > the day.of.week function. But day.of.week() doesn't eat a chron > object, he insists he wants m,d,y. This seems quite odd. Any idea what > I can do?Chron and date are older packages, you may want to use the more recent (and very powerful) DateTimeClasses> parsedDate <- strptime("01-03-04", "%d-%d-%y") > format(parsedDate)[1] "2004-01-03"> class(parsedDate)[1] "POSIXt" "POSIXlt"> weekdays(parsedDate)[1] "Saturday" Start with help(DateTimeClasses), if you ever used the C functions strptime and strftime it shouldn't be too foreign. And do look at the mailing list archives (and/or Google), as questions get answered on this quite often. Dirk -- The relationship between the computed price and reality is as yet unknown. -- From the pac(8) manual page
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 SundayThanks! 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
month.day.year takes a chron object and produces a list with three components named month, day and year. with executes its second argument in an environment where the components of the list of the first argument are variables so arg 2 can refer to the names month, day and year. --- Date: Thu, 4 Mar 2004 00:03:59 +0530 From: Ajay Shah <ajayshah at mayin.org> To: Gabor Grothendieck <ggrothendieck at myway.com> Cc: <r-help at stat.math.ethz.ch> Subject: Re: [R] Find out the day of week for a chron object? [...] 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
You can avoid the loop in your calculation. Since Friday is day of the week 5, if the day of the week d of chron date x is greater than 5 then Friday was d-5 days ago; otherwise, if d-5 is zero or negative then we have to add 7 to it to ensure that its in the past. Thus: prevFriday <- function(x) { d <- with(month.day.year(x), day.of.week(month,day,year)) x - ifelse( d-5>0, d-5, d-5+7 ) } Date: Thu, 4 Mar 2004 00:03:59 +0530 From: Ajay Shah <ajayshah at mayin.org> To: Gabor Grothendieck <ggrothendieck at myway.com> Cc: <r-help at stat.math.ethz.ch> Subject: Re: [R] Find out the day of week for a chron object? 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 SundayThanks! 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