[R] Convert date to continuous variable in R

Ahmed Attia ahmedatia80 at gmail.com
Thu Jul 6 16:42:55 CEST 2017


Thanks it worked for me. I wanted to plot days since planting on
x-axis 1 and years on x-axis 3.

LAI_simulation$Date <- as.Date( LAI_simulation$Date, '%Y/%m/%d')
LAI_simulation$Date <- as.integer(LAI_simulation$Date - as.Date("2009-10-07"))
plot(LAI~Date,data=LAI_simulation,xlab="Days since Oct, 7,
2009",cex=0.3,cex.lab=1.2,xaxt="n",xaxs="i",yaxs="i",ylim=c(0,6))
axis(1,xlim=c(0,2200),xaxp=c(0,2200,11),cex.lab=1.2,cex.axis=0.8,xaxs="i")
LAI_simulation$Date<- as.Date(LAI_simulation$Date, origin="2009-10-07")
par(new=TRUE)
plot(LAI~Date, data=LAI_simulation,xlab="",ylab="",
     xaxt="n",type="p",cex=0.2,col=1,xaxs="i",yaxs="i",ylim=c(0,6))
axis.Date(side=3, at=seq(min(LAI_simulation$Date),
max(LAI_simulation$Date),by="year"), format="%Y",
xlab="xxx",cex.axis=0.9)
mtext("years",side=3,line=3,cex.lab=1.2)
Ahmed Attia, Ph.D.
Agronomist & Soil Scientist






On Wed, Jul 5, 2017 at 4:52 PM, MacQueen, Don <macqueen1 at llnl.gov> wrote:
> Not entirely sure what you really want the axis on the top to look like, but here's what I'd be likely to do:
>
> ## note, correction to format; in your example data year comes last
> LAI_simulation$Date <- as.Date( LAI_simulation$Date, '%m/%d/%Y')
>
> plot(LAI~Date, data=LAI_simulation)
>
> simdate <- as.numeric( LAI_simulation$Date - as.Date('2009-10-7') )
> ## or
> simdate <- difftime( LAI_simulation$Date, as.Date('2009-10-7') )
>
> ## then
> axis(3, pretty(simdate) )
>
>
> Converting LAI_simulation$Date to numeric, and then applying axis.Date() to it makes no sense to me, since the elapsed numbers of days are not dates.
>
> --
> Don MacQueen
>
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
>
>
> On 7/5/17, 11:23 AM, "Ahmed Attia" <ahmedatia80 at gmail.com> wrote:
>
>     How can I convert date to continuous variable staring from 1. I tried
>     as.numeric but R starts counting from default January 1, 1970 while my data
>     start Oct 7, 2009.
>
>     I need to do this conversion to plot the date on two x-axes (1 and 3).
>     Ex;
>     Date LAI
>     10/7/2009 0
>     10/8/2009 0.004173
>     10/9/2009 0.009997
>     10/10/2009 0.009998
>     10/11/2009 0.009999
>     10/12/2009 0.009999
>     10/13/2009 0.009999
>     10/14/2009 0.009999
>     10/15/2009 0.009998
>     10/16/2009 0.009998
>     10/17/2009 0.009998
>     10/18/2009 0.009998
>     10/19/2009 0.009998
>     10/20/2009 0.009998
>
>     LAI_simulation$Date <- as.Date( LAI_simulation$Date, '%Y/%m/%d')
>     plot(LAI~Date, data=LAI_simulation)
>     LAI_simulation$Date<- as.numeric(LAI_simulation$Date,origin="2009-10-07")
>     #R does not see the origin
>     axis.Date(side=3,at=LAI_simulation$Date)
>
>
>
>
>     Ahmed Attia, Ph.D.
>     Agronomist & Soil Scientist
>
>         [[alternative HTML version deleted]]
>
>     ______________________________________________
>     R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>     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.
>
>



More information about the R-help mailing list