Skip to content

Date conversion

4 messages · Pele, Sundar Dorai-Raj, Uwe Ligges

#
Hi R users,

I have a factor variable called date as shown below:  Can anyone share the
best / most efficient way to extract year and week (e.g.  year = 2006, week
= 52 for first record, etc..)?  My data set has 1 million records.

DATE        
11DEC2006 
11SEP2006
01APR2007
02DEC2007


Thanks in advance for any help!
#
Hi,

There are possibly several ways to do this. My approach would be:

dates <- strptime(as.character(DATE), "%d%b%Y")
year <- dates$year + 1900
week <- floor(dates$yday/365 * 52)

HTH,

--sundar
On Thu, Mar 5, 2009 at 8:58 AM, Pele <drdionc at yahoo.com> wrote:
#
Pele wrote:
Since I am not in the correct locale:

Sys.setlocale(locale="C")
date <- strptime(DATE, "%d%B%Y")
format(date, "%Y")
format(date, "%W") # which is certainly not 52

Uwe Ligges
#
Hi Uwe,

You are correct - that was a type O (52) and thanks for you your suggestion
that works..
Pele wrote: