创建日期时间时间序列对象和绘图

时间:2017-11-20 20:31:38

标签: r datetime time xts series

我有很多温度数据以15分钟的间隔连续采集,单位为dd / mm / yyyy hh:mm:ss。我是R的新手,我正在努力绘制数据。我写的代码是:

install.packages("xts")
library("xts")

temp<-read.csv("C:\\Users\\data\\Temp Data.csv",header=TRUE)
str(temp)

temp$DateTime<-as.POSIXct(strptime(temp$DateTime,"%m/%d/%Y %H:%M"))

temp.xts<-xts(temp,order.by=temp$DateTime)
summary(temp.xts)

par(mfrow=c(1,1))

Temp.lab=seq(5,30,by=5)
Temp.ticks=seq(5,30,by=5)
plot(temp.xts$Temp.C["2015-05-01/2015-11-5"],axes=F,auto.grid=FALSE,col="gray48",ylim=c(5,30),main="",cex.main=1.0,lwd=1)
axis(2,at=Temp.ticks,labels=format(Temp.lab,scientific=FALSE),ylab="Temperature (C)",las=1,cex.axis=1)
mtext("Water Temperatuer",side=3,line=-1.25,cex=1,font=2,las=1,adj=0.025)
mtext("",side=2,line=3,las=3,cex=1)
mtext("",side=1,line=3,cex=1)

当我运行这些时,我得到错误:plot.xts中的错误(temp.xts $ Temp.C [&#34; 2015-05-01 / 2015-11-15&#34;],axes = F, :   &#39; X&#39;必须是一个时间序列的对象。

我的数据结构如下;

  

头(TEMP)     Station.ID DateTime Temp.C   1 Station.01 2015-05-08 14:00:00 14.002   2 Station.01 2015-05-08 14:15:00 13.906   3 Station.01 2015-05-08 14:30:00 13.978   4 Station.01 2015-05-08 14:45:00 14.026   5 Station.01 2015-05-08 15:00:00 14.074   6 Station.01 2015-05-08 15:15:00 14.098

     

STR(TEMP)   &#39; data.frame&#39;:18283 obs。 3个变量:    $ Station.ID:因子w / 1级&#34; Station.01&#34;:1 1 1 1 1 1 1 1 1 1 ...    $ DateTime:因子w / 18279水平&#34; 10/1/2015 00:00&#34;,..:6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 ...    $ Temp.C:num 14 13.9 14 14 14.1 ...

以下是我的数据集的片段:

头(温度,20)         Station.ID DateTime Temp.C      1 Station.01 5/8/2015 14:00 14.002      2 Station.01 5/8/2015 14:15 13.906      3 Station.01 5/8/2015 14:30 13.978      4 Station.01 5/8/2015 14:45 14.026      5 Station.01 5/8/2015 15:00 14.074      6 Station.01 5/8/2015 15:15 14.098      7 Station.01 5/8/2015 15:30 14.122      8 Station.01 5/8/2015 15:45 14.146      9 Station.01 5/8/2015 16:00 14.146      10 Station.01 5/8/2015 16:15 14.146      11 Station.01 5/8/2015 16:30 14.146      12 Station.01 5/8/2015 16:45 14.146      13 Station.01 5/8/2015 17:00 14.122      14 Station.01 5/8/2015 17:15 14.122      15 Station.01 5/8/2015 17:30 14.122      16 Station.01 5/8/2015 17:45 14.098      17 Station.01 5/8/2015 18:00 14.122      18 Station.01 5/8/2015 18:15 14.098      19 Station.01 5/8/2015 18:30 14.098      20 Station.01 5/8/2015 18:45 14.098

 Any suggestions? Help is greatly appreciated.

1 个答案:

答案 0 :(得分:4)

我有同样的错误&#34;&#39; x&#39;必须是一个时间序列的对象。&#34;对于我在debian系统中的一个(看起来类似的)脚本(同时在一个suse框中它很好)和 &#34;解决&#34;它通过使用as.numeric显式转换为数字。在你的情况下,像plot(as.numeric(temp.xts$Temp.C["2015-05-01/2015-11-5"]),.....

相关问题