[R] Write date class as number of days from 1970

arun smartpink111 at yahoo.com
Fri May 3 16:43:56 CEST 2013


Hi,
May be this helps:
set.seed(24)
dat1<- data.frame(date1=sample(seq(as.Date("2012-09-14",format="%Y-%m-%d"),length.out=40,by="day"),20,replace=FALSE), value=sample(1:60,20,replace=TRUE))
dat1$days1<- as.numeric(difftime(dat1$date1,as.Date("1970-01-01")))
#or
library(lubridate) 
dat1$days2<- days(dat1$date1)$day
head(dat1)
#       date1 value days1 days2
#1 2012-09-25     6 15608 15608
#2 2012-09-22    34 15605 15605
#3 2012-10-10    44 15623 15623
#4 2012-10-03     9 15616 15616
#5 2012-10-07    14 15620 15620
#6 2012-10-16    42 15629 15629
#or
library(chron)
as.numeric(as.chron(dat1$date1)-chron(0))
 #[1] 15608 15605 15623 15616 15620 15629 15606 15622 15631 15604 15615 15607
#[13] 15626 15624 15635 15619 15601 15598 15636 15599


A.K.

>Dear all, 
>
>I have a dataset with one column being of class Date. When I 
write the output, I would like that column being written as number of 
days from >1970-01-01. I could not find anywhere a way to do it. 
>
>Thanks, 
>Marco



More information about the R-help mailing list