r - 在R中选择一个带窗口的时间段;格式 "dd/mm/yyyy h:m:s"(Windows 7)

标签 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")

最佳答案

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 对象划分子集。请注意,Modellwind.zoochron 索引的原点是 1949-12-01,而不是默认的 chron 原点 1990-01-01,因此您需要指定它是为了避免有关来源不匹配的警告。

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 

关于r - 在R中选择一个带窗口的时间段;格式 "dd/mm/yyyy h:m:s"(Windows 7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42322933/

相关文章:

Rollapply 在 R 时间范围索引中应用

r - 我如何找到一个值在列中重复了多少次 - R

R验证源代码

r - 遍历两个表(矩阵)匹配列并在 R 中应用函数

Python Timedelta 算术与 noleap 日历

R Zoo 包 : na. 当 NA 处于开头时大约改变向量的长度

r - 如何使用 facet_wrap 绘制 ggplot2,显示每个组的百分比,而不是整体百分比?

R 错误消息 - "arguments imply differing number of rows"

r - 如何将数据帧行迭代转换为时间序列对象并将 auto.arima 应用于每一行

r - 计算在给定级别发生变化时花费的时间