[R] Find out the day of week for a chron object?

Gabor Grothendieck ggrothendieck at myway.com
Wed Mar 3 13:25:27 CET 2004



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




More information about the R-help mailing list