从R中的观测中选择日期和时间间隔

时间:2013-01-25 16:00:51

标签: r time-series

拥有类动物园的对象,我们可以使用函数选择一系列感兴趣日期的观察结果:

window(z, start = as.Date("2006-01-05"), end = as.Date("2006-01-08"))

在运行此功能时,会出现以下警告消息:

Warning messages:
1: In which(in.index & all.indexes >= start & all.indexes <= end) :
  Metodi incompatibili ("Ops.POSIXt", "Ops.Date") per ">="
2: In which(in.index & all.indexes >= start & all.indexes <= end) :
  Metodi incompatibili ("Ops.POSIXt", "Ops.Date") per "<="

我已经检查过该对象是类动物园,并且日期包含在时间序列中。

这怎么可能?

根据要求在str(z)下面:

   ‘zoo’ series from 2006-01-03 to 2013-01-24
  Data: num [1:1795, 1:40] 3.65 3.68 3.69 3.72 3.7 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:1795] "1" "2" "3" "4" ...
  ..$ : chr [1:40] "EURARS" "EURAUD" "EURBRO" "EURCAD" ...
  Index:  POSIXct[1:1795], format: "2006-01-03" "2006-01-04" "2006-01-05" "2006-01-06" ...

根据要求在dput(head(z))下面:

structure(c(3.6511, 3.6833, 3.6931, 3.7152, 3.7027, 3.6897, 1.62349, 
1.62257, 1.62011, 1.6115, 1.60243, 1.61108, 2.802, 2.7692, 2.7727, 
2.7741, 2.7238, 2.729, 1.38937, 1.39109, 1.40716, 1.41627, 1.41196, 
1.40666, 1.55055, 1.5472, 1.5448, 1.54335, 1.54215, 1.545, 623.73, 
624.16, 628.43, 638.11, 632.27, 630.7, 9.6988, 9.7803, 9.7689, 
9.802, 9.7492, 9.7354, 2742.03, 2765.68, 2758.65, 2769.27, 2753.3, 
2747.31, 29.047, 28.972, 28.9, 28.88, 28.764, 28.792, 7.4616, 
7.4601, 7.4612, 7.458, 7.46, 7.4589, 6.8983, 6.9551, 6.9594, 
6.9838, 6.9374, 6.9253, 0.6882, 0.68905, 0.68961, 0.6863, 0.68473, 
0.68358, 9.3178, 9.3963, 9.3889, 9.4207, 9.3702, 9.3516, 251.72, 
250.28, 250.66, 250.39, 249.89, 250.86, 11657.46, 11677.26, 11612.05, 
11603.59, 11433.35, 11403.84, 5.5244, 5.5808, 5.5799, 5.6134, 
5.5858, 5.5957, 53.5288, 54.0151, 54.0323, 54.0105, 53.5591, 
53.6189, 74.96, 74.88, 74.41, 73.94, 73.79, 73.84, 139.6, 140.71, 
140.39, 139.09, 138.39, 137.93, 1208.3493, 1210.0214, 1195.7746, 
1200.3966, 1181.3457, 1184.9635, 160.65, 162.15, 162.02, 162.53, 
161.7, 161.43, 10.9802, 10.997, 10.9947, 10.9635, 10.9909, 10.9874, 
12.7724, 12.8255, 12.8746, 12.8338, 12.7859, 12.8273, 4.5416, 
4.5702, 4.5623, 4.5597, 4.5308, 4.5229, 7.9654, 7.9248, 7.9254, 
7.914, 7.9574, 8.0011, 1.7571, 1.7626, 1.7622, 1.7574, 1.7411, 
1.7391, 4.1276, 4.1608, 4.1665, 4.1818, 4.1606, 4.1534, 63.2627, 
63.4733, 63.6725, 63.7198, 63.3608, 63.3412, 3.8295, 3.8116, 
3.807, 3.805, 3.7609, 3.7799, 3.6732, 3.6815, 3.6834, 3.6842, 
3.6644, 3.6492, 34.5475, 34.8363, 34.7254, 34.8369, 34.68, 34.39, 
9.3648, 9.3279, 9.3321, 9.3152, 9.3389, 9.3603, 1.9844, 1.9932, 
1.9938, 1.9902, 1.9766, 1.9716, 48.9853, 48.9426, 48.6762, 48.3184, 
47.9995, 48.0187, 1.6149, 1.6195, 1.6193, 1.6204, 1.6155, 1.6148, 
1.6129, 1.6175, 1.6184, 1.6201, 1.6182, 1.6221, 39.2261, 39.1868, 
38.7569, 39.1189, 38.6148, 38.6309, 6.0673, 6.114, 6.1208, 6.1484, 
6.1095, 6.1082, 1.2019, 1.2119, 1.211, 1.2151, 1.2088, 1.2065, 
7.4834, 7.4559, 7.4658, 7.3872, 7.3206, 7.3497), .Dim = c(6L, 
40L), .Dimnames = list(c("1", "2", "3", "4", "5", "6"), c("EURARS", 
"EURAUD", "EURBRO", "EURCAD", "EURCHF", "EURCLP", "EURCNO", "EURCOP", 
"EURCZK", "EURDKK", "EUREGP", "EURGBP", "EURHKD", "EURHUF", "EURIDO", 
"EURILS", "EURINO", "EURISK", "EURJPY", "EURKRO", "EURKZT", "EURMAD", 
"EURMXN", "EURMYO", "EURNOK", "EURNZD", "EURPEN", "EURPHO", "EURPLN", 
"EURRON", "EURRUB", "EURSEK", "EURSGO", "EURTHO", "EURTND", "EURTRY", 
"EURTWO", "EURUAH", "EURUSD", "EURZAR")), index = structure(c(1136242800, 
1136329200, 1136415600, 1136502000, 1136761200, 1136847600), class = c("POSIXct", 
"POSIXt"), tzone = ""), class = "zoo")

2 个答案:

答案 0 :(得分:2)

您不应该将DatePOSIXct

进行比较

试试这个:

window(z, start = as.POSIXct("2006-01-05"), end = as.POSIXct("2006-01-08"))

或者,当@JoshuaUlrich指出comment时,您的数据是每日频率,因此您最好为您的数据使用Date索引类,以避免时区怪异

index(z) <- as.Date(index(z))
window(z, start = as.Date("2006-01-05"), end = as.Date("2006-01-08"))

答案 1 :(得分:0)

看起来window不会自动在DatePOSIXct之间进行转换。您必须在与数据相同的类中指定时间:

window(z, start = as.POSIXct("2006-01-05"), end = as.POSIXct("2006-01-08"))
                    EURARS EURAUD EURBRO  EURCAD  EURCHF EURCLP EURCNO  EURCOP
2006-01-05 23:00:00 3.7152 1.6115 2.7741 1.41627 1.54335 638.11  9.802 2769.27
                    EURCZK EURDKK EUREGP EURGBP EURHKD EURHUF   EURIDO EURILS
2006-01-05 23:00:00  28.88  7.458 6.9838 0.6863 9.4207 250.39 11603.59 5.6134
                     EURINO EURISK EURJPY   EURKRO EURKZT  EURMAD  EURMXN
2006-01-05 23:00:00 54.0105  73.94 139.09 1200.397 162.53 10.9635 12.8338
                    EURMYO EURNOK EURNZD EURPEN  EURPHO EURPLN EURRON  EURRUB
2006-01-05 23:00:00 4.5597  7.914 1.7574 4.1818 63.7198  3.805 3.6842 34.8369
                    EURSEK EURSGO  EURTHO EURTND EURTRY  EURTWO EURUAH EURUSD
2006-01-05 23:00:00 9.3152 1.9902 48.3184 1.6204 1.6201 39.1189 6.1484 1.2151
                    EURZAR
2006-01-05 23:00:00 7.3872