[R] date & time manipulation- R 2.15.1 windows 7

Gabor Grothendieck ggrothendieck at gmail.com
Thu Mar 14 18:08:49 CET 2013


On Thu, Mar 14, 2013 at 9:45 AM, yash kajaria <yash.kajaria at gmail.com> wrote:
> Hi,
>     I wanted to learn how to solve a date and time manipulation where i can
> do the following two
>     1. difference of two dates eg (differnce between 5th jan 2013 and 1st
> jan 2013)
>
>     2.Suppose i have week number of the year, i want to know if i can find
> out the day it refers to eg( say week 2 of 2013 would be  6th jan 2013 and
> the day is sunday)
>        i need my result to tell me that its the 6th of jan 2013 as well as
> the day (sunday)

Try this:

> dif <- d - as.Date(cut(d, "year"))
> dif
Time difference of 4 days
> as.numeric(dif)
[1] 4

The year and week don't imply the day of the week.  We can input the
year, week number and day of the week to get the date:

> year <- 2013
> week <- 2
> day <- "Sun"
> as.Date(paste(year, week-1, day), "%Y %W %a")
[1] "2013-01-06"


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list