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 -- View this message in context: http://r.789695.n4.nabble.com/Write-date-class-as-number-of-days-from-1970-tp4666155.html Sent from the R help mailing list archive at Nabble.com.
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
On 03.05.2013 15:59, Manta wrote:> 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.as.numeric(x) where x is the Date object. Uwe Ligges> Thanks, > Marco > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Write-date-class-as-number-of-days-from-1970-tp4666155.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >