【问题标题】:convert date to continuous variable in R将日期转换为R中的连续变量
【发布时间】:2017-07-06 22:22:40
【问题描述】:

如何将日期转换为从 1 开始的连续变量。我尝试使用 as.numeric 但 R 从默认的 1970 年 1 月 1 日开始计数,而我的数据从 2009 年 10 月 7 日开始。

我需要进行此转换以在两个 x 轴(1 和 3)上绘制日期。

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)
# also tried seq
dates <- seq(LAI_simulation$Date[1, "2009-10-07"], LAI_simulation$Date[nrow(LAI_simulation$Date), "2010-10-07"], by = "days")

感谢您的帮助。

【问题讨论】:

  • 只需减去基准日期,然后像LAIsimulation$index &lt;- as.integer(LAI_simulation$Date - as.Date("2009-10-06")) 一样转换。
  • 感谢为我工作。

标签: r date


【解决方案1】:
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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 2020-09-16
    • 2013-04-15
    • 2018-12-16
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多