选择R中窗口的时间段;格式“dd / mm / yyyy h:m:s”(Windows 7)

时间:2017-02-19 02:18:02

标签: r time-series zoo chron anytime

我有以下问题。我想使用以下代码为每日数据选择一个特定的时间段,例如:

window(Modellwind.zoo, start = as.Date("01/Jan/2001 12:00:00"), end = as.Date("4/Jan/2001 12:00:00"))

我收到以下错误消息: charToDate(x)中的错误:   字符串不是标准的明确格式

当我尝试使用函数anydate找到可接受的时间格式时,它也没有工作:

library(anytime)
anydate("01/Jan/2001 12:00:00")

使用以下代码:

window(Modellwind.zoo, start = as.Date("2001-01-01"), end = as.Date("2001-01-04")) 

不幸的是,我收到以下错误:

 **Warning messages:
  1: In which(in.index & all.indexes >= start & all.indexes <= end) :
  Incompatible methods ("Ops.dates", "Ops.Date") for ">="
  2: In which(in.index & all.indexes >= start & all.indexes <= end) :
  Incompatible methods ("Ops.dates", "Ops.Date") for "<="**

以下代码包含Modellwind.zoo的示例。

structure(c(9.08093655399134, 6.51590181162631, 7.14637946155745, 
1.43900253813098, 6.78880326680026, 14.3182887821646, 16.3360242476697, 
16.1781018622214, 17.2200845065928, 15.6439142273171, 8.10504553259712, 
3.78898221928137, 6.78608582121557, 7.18116948778303, 5.0299974451978, 
3.49148782050232, 6.9941692218925, 8.45512766287497, 12.0693672354131, 
11.987955907515, 10.3290912344961, 13.4506307038479, 21.7989491163794, 
14.0085737502259, 14.5883127217965, 11.8048508250059, 24.7915690531695, 
19.151192329502, 12.1739793389357, 11.9410486288817, 20.9967608089789, 
15.2111025271479, 5.90129944159158, 2.42733488656831, 7.20743282263504, 
22.737089035552, 14.8219437253637, 14.0558804343021, 8.98137356225915, 
12.9592918632241, 18.4870237580719, 9.11790624009415, 2.27721679625411, 
2.61125956054424, 3.26998407545227, 5.35392572192135, 4.95193258824599, 
6.86224460928498, 9.06594694653957, 12.4505570716657, 12.740653858499, 
15.8771799446521, 12.5618618366812, 3.58848453998801, 10.9966305297934, 
4.86674413518877, 10.7031531327265, 16.2043681264107, 12.0587344974091, 
2.10949588086561), index = structure(c(18659.5, 18660.5, 18661.5, 
18662.5, 18663.5, 18664.5, 18665.5, 18666.5, 18667.5, 18668.5, 
18669.5, 18670.5, 18671.5, 18672.5, 18673.5, 18674.5, 18675.5, 
18676.5, 18677.5, 18678.5, 18679.5, 18680.5, 18681.5, 18682.5, 
18683.5, 18684.5, 18685.5, 18686.5, 18687.5, 18688.5, 18689.5, 
18690.5, 18691.5, 18692.5, 18693.5, 18694.5, 18695.5, 18696.5, 
18697.5, 18698.5, 18699.5, 18700.5, 18701.5, 18702.5, 18703.5, 
18704.5, 18705.5, 18706.5, 18707.5, 18708.5, 18709.5, 18710.5, 
18711.5, 18712.5, 18713.5, 18714.5, 18715.5, 18716.5, 18717.5, 
18718.5), .Dim = 60L, format = structure(c("dd/mm/yyyy", "h:m:s"
), .Names = c("dates", "times")), origin = c(12L, 1L, 1949L), class = c("chron", 
"dates", "times")), class = "zoo")

1 个答案:

答案 0 :(得分:3)

Modellwind.zoo的索引为chron。这就是为什么您按Date进行分组的尝试不成功的原因(您的第一次尝试失败,因为as.Date需要%Y-%m-%d格式)。在按Date

进行子集之前,将索引更改为Date
Modellwind.zoo.Date <- Modellwind.zoo
index(Modellwind.zoo.Date) <- as.Date(index(Modellwind.zoo.Date))
window(Modellwind.zoo.Date, start = as.Date("2001-01-01"), end = as.Date("2001-01-04"))
# 2001-01-01 2001-01-02 2001-01-03 2001-01-04 
#   9.080937   6.515902   7.146379   1.43900

或者将索引保留为chron并将chron个对象保留为子集。请注意,chronModellwind.zoo索引的来源是1949-12-01,而不是1990-01-01的默认chron来源,因此您需要指定它以避免发出错误警告匹配的起源。

beg.chron <- as.chron("01/01/2001", origin = c(12, 1, 1949))
end.chron <- as.chron("01/04/2001", origin = c(12, 1, 1949))
window(Modellwind.zoo, start = beg.chron, end = end.chron)
# (01/Jan/2001 12:00:00) (02/Jan/2001 12:00:00) (03/Jan/2001 12:00:00) 
#               9.080937               6.515902               7.146379