从XTS转换为Zooreg时间序列会删除日期值

时间:2019-03-13 21:13:23

标签: r time-series xts zoo

希望您过得愉快!

首先,这是我的数据的目标:

StreamsTempAveragextsMonthly <- structure(c(16.44, 15.7230769230769, 16.4484358974359, 16.2202307692308, 
15.6025, 16.4114423076923, 16.7097115384615, 16.32125, 16.7625, 
16.8855769230769, 17.864358974359, 18.4282692307692, 17.5625, 
16.9068269230769, 17.1636730769231, 16.8279230769231, 17.09125, 
17.8747916666667, 17.2025, 16.9225, 17, 17.75, 17.85, 17.3663461538462, 
17.4355769230769, 16.8797115384615, 17.0658717948718, 17.2979230769231, 
17.2128205128205, 17.78225, 17.48, 16.9457051282051, 18.535, 
18.5871634615385, 17.7346153846154, 18.214188034188, 16.7875, 
16.6706196581197, 17.103125, 17.3691346153846, 17.8264423076923, 
16.4920192307692, 16.8905128205128, 16.9484615384615, 17.350641025641, 
17.9035096153846, 18.1136363636364, 18.0958, 18.125, 17.3089871794872, 
17.3978205128205, 17.8078985507246, 17.6580384615385, 17.7281643356643, 
17.3222222222222, 17.6125555555556, 18.4708333333333, 18.9261363636364, 
18.3610714285714, 18.2842857142857, 17.5, 18.9776818181818, 18.2313068181818, 
18.5352272727273, 18.2314393939394, 16.8462121212121, 17.5330452568202, 
16.5851136363636, 18.3415151515152, 19.0620454545455, 17.5744047619048, 
16.7176308539945, 16.6407407407407, 16.7227272727273, 16.4184848484848, 
17.5290598290598, 17.1817965367965, 16.4547619047619, 15.9484848484848, 
15.6835902503294, 16.0388865398168, 17.0166666666667, 17.5905555555556, 
16.4290088383838, 16.2997452016069, 16.1557023172906, 16.7221212121212, 
16.857196969697, 17.3215277777778, 16.5125, 16.0752525252525, 
15.5828743589744, 16.6117845117845, 17.9668930686172, 17.5651666666667, 
16.3064442224442, 16.2763888888889, 17.650505050505, 16.8803872053872, 
17.3298611111111, 17.3772443181818, 17.0242424242424, 16.8111111111111, 
16.8055555555556, 17.232601010101, 17.8261363636364, 17.3753787878788, 
16.5777272727273, 15.8893939393939, 16.9522435897436, 16.5363636363636, 
16.6034090909091, 17.2667929292929, 18.0511363636364, 17.5534090909091, 
16.4551136363636, 17.23125, 17.5136363636364, 17.8482954545455, 
16.9869318181818, 16.4244318181818, 17.125, 16.8022727272727, 
17.4343006993007, 17.2990909090909, 17.1961538461538, 17.009375, 
16.9174747474747, 17.2710227272727, 17.6715909090909, 17.4818181818182, 
15.9962121212121, 16.5631818181818, 16.6078349282297, 17.1787878787879, 
17.0653409090909, 17.4431818181818, 17.4880681818182, 15.0918803418803, 
16.7741339869281, 17.4332792207792, 17.7990981240981, 18.0886363636364, 
16.8297619047619, 16.6851973684211, 17.2556818181818, 17.0295454545455, 
16.8240909090909, 16.7538770053476, 16.2579545454545, 15.9655757575758, 
17.1280753968254, 17.7519191919192, 17.4414335664336, 17.4557954545455, 
16.1622115384615, 16.8327651515152, 16.9667613636364, 16.672798573975, 
16.9659090909091, 17.0818181818182, 17.1822027972028, 17.8164884135472, 
17.4545454545455, 16.7771212121212, 17.8238636363636, 17.7005244755245, 
16.7160984848485, 16.341754756871, 16.6440025252525, 16.9829545454545, 
16.6630555555556, 19.1123467230444, 19.3261363636364, 17.7603021978022, 
18.8059107142857, 18.2065559440559, 18.25, 16.965, 18.0582115800866, 
17.2105637254902, 18.6762012987013, 18.4136363636364, 18.2600378787879, 
18.0003496503497, 17.9288825757576, 17.959375, 18.5164772727273, 
18.4772727272727, 18.2170454545455, 18.90625, 16.7813725490196, 
17.2660256410256, 17.0635542929293, 17.2422902097902, 17.1887820512821, 
16.1727941176471, 16.0860294117647, 18.3868181818182, 18.0298076923077, 
18.7031536293164, 18.3659090909091, 18.625, 16.7256818181818, 
17.2232600732601, 18.3319639192886, 17.9238636363636, 17.9799422799423, 
16.5837121212121, 16.100952540107, 16.9387019230769, 17.3568181818182, 
17.1753146853147, 17.1159090909091, 18.1145833333333, 16.8415170940171, 
16.9133597285068, 17.6888034188034, 17.1259793447293, 17.384655448718, 
16.7841346153846, 16.2973468660969, 16.1201495726496, 17.5849893162393, 
18.2456018518519, 18.0254807692308, 17.4643233618234, 17.1731532356532, 
17.5413283475783, 17.7156695156695, 17.5869255189255, 17.6197802197802, 
17.1997996794872, 17.6756588319088, 18.048433048433, 18.5746082621083, 
19.1991987179487, 18.6432081807082, 16.8430288461538, 17.4325367647059, 
17.314707977208, 17.5010576923077, 17.9123219373219, 17.4, 17.5033333333333, 
17.8433333333333, 18.9353846153846, 18.6049679487179, 17.1923076923077, 
17.4415196078431, 17.6505057932264, 17.2357352941176, 17.9955326484661, 
17.5959523809524, 17.5952941176471, 16.9622171945701, 18.2641666666667, 
17.3842124183007, 18.8371301247772, 18.2442091503268, 17.8693572984749, 
17.4261783559578, 17.8408721670486, 18.0430294117647, 17.5234615384615, 
18.7757894736842, 18.0545833333333, 18.3861029411765, 17.2286764705882, 
19.1830882352941, 18.9825490196078, 19.399375, 18.8928921568627, 
17.379375, 17.8381315789474, 18.9048611111111, 18.32625, 18.5, 
17.1804924242424, 17.165, 19.0856617647059, 18.6882066993464, 
18.949358974359, 18.2374264705882, 17.9036554621849, 17.7655882352941, 
18.0464795008913, 19.0757010582011, 18.0125, 17.0503365384615, 
17.4858707264957, 18.0303062678063, 19.0284455128205, 18.0894444444444, 
18.4038461538462), .Dim = c(295L, 1L), .Dimnames = list(NULL, 
    "AverageTemp"), index = structure(c(741398400, 742003200, 
746668800, 749347200, 751766400, 754185600, 757209600, 759628800, 
762048000, 765072000, 767491200, 770256000, 772243200, 775353600, 
778118400, 780710400, 783216000, 785635200, 788054400, 791424000, 
793238400, 796521600, 798940800, 801360000, 803865600, 807148800, 
809740800, 812246400, 814406400, 817603200, 819244800, 823046400, 
824860800, 827884800, 830131200, 833328000, 835315200, 838771200, 
841190400, 843609600, 846633600, 849052800, 851904000, 854496000, 
857088000, 859420800, 861321600, 864864000, 867110400, 870220800, 
872640000, 875491200, 878083200, 880502400, 883353600, 885945600, 
888364800, 890784000, 893808000, 895190400, 900115200, 904521600, 
907027200, 909532800, 911952000, 913161600, 916790400, 919555200, 
921715200, 925257600, 928108800, 930700800, 933292800, 935712000, 
938131200, 941155200, 942969600, 945734400, 949017600, 951436800, 
953856000, 956016000, 958694400, 962064000, 964483200, 966988800, 
970185600, 972604800, 974937600, 977443200, 980208000, 982800000, 
985910400, 987984000, 991267200, 993772800, 994723200, 999216000, 
1001548800, 1004054400, 1006473600, 1008892800, 1012435200, 1014854400, 
1017446400, 1019260800, 1022803200, 1025308800, 1027728000, 1030147200, 
1032566400, 1034985600, 1038614400, 1039824000, 1043452800, 1045872000, 
1048291200, 1050624000, 1053129600, 1056758400, 1059177600, 1060387200, 
1064016000, 1067558400, 1070064000, 1071273600, 1074902400, 1077580800, 
1079740800, 1083283200, 1084579200, 1087948800, 1091232000, 1093651200, 
1096070400, 1098489600, 1100908800, 1103328000, 1106352000, 1108771200, 
1111190400, 1114819200, 1116028800, 1117843200, 1122076800, 1124496000, 
1128038400, 1130544000, 1133222400, 1135209600, 1138665600, 1140825600, 
1143244800, 1145664000, 1148083200, 1151107200, 1154131200, 1156550400, 
1158969600, 1161388800, 1163808000, 1166227200, 1169251200, 1171670400, 
1175299200, 1177718400, 1180051200, 1183161600, 1185580800, 1.188e+09, 
1190419200, 1191628800, 1196294400, 1197676800, 1200614400, 1203120000, 
1206748800, 1209168000, 1210377600, 1214006400, 1216339200, 1220054400, 
1222473600, 1224892800, 1227312000, 1229731200, 1232755200, 1235174400, 
1237507200, 1240012800, 1242432000, 1248480000, 1250553600, 1253318400, 
1256947200, 1259280000, 1260576000, 1262995200, 1266710400, 1269043200, 
1272585600, 1275091200, 1275350400, 1280534400, 1282953600, 1285372800, 
1287792000, 1290211200, 1292630400, 1295568000, 1298073600, 1299283200, 
1304035200, 1306454400, 1307664000, 1311984000, 1314316800, 1316822400, 
1319241600, 1322611200, 1324080000, 1327104000, 1330473600, 1333152000, 
1335571200, 1338249600, 1340323200, 1342828800, 1346371200, 1348790400, 
1351209600, 1353628800, 1356134400, 1359158400, 1360368000, 1363910400, 
1366416000, 1369699200, 1375228800, 1377648000, 1379635200, 1382486400, 
1384992000, 1386892800, 1391126400, 1393545600, 1395360000, 1398297600, 
1401235200, 1406246400, 1408579200, 1412035200, 1414713600, 1417046400, 
1418428800, 1421712000, 1424217600, 1426723200, 1430352000, 1432944000, 
1435276800, 1438214400, 1440979200, 1443398400, 1444867200, 1447372800, 
1449619200, 1453334400, 1456704000, 1459382400, 1461715200, 1463011200, 
1466726400, 1469059200, 1472601600, 1475020800, 1477440000, 1479168000, 
1481155200, 1484179200, 1487721600, 1489968000, 1491436800, 1495065600, 
1498780800, 1501027200, 1503446400, 1508544000, 1510963200, 1513296000, 
1516233600, 1519171200, 1520812800, 1523318400, 1525219200), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", class = c("xts", 
"zoo"))

它具有以下结构:

 An ‘xts’ object on 1993-06-30/2018-05-02 containing:
  Data: num [1:295, 1] 16.4 15.7 16.4 16.2 15.6 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "AverageTemp"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL

请注意最后一次约会日期是2018-05-02?现在,问题是当我将xts对象转换为zooreg对象时。我使用了这段代码:

 StreamsTempzooreg <- zooreg(StreamsTempAveragextsMonthly, start = c(1993,6), end = c(2018,5), frequency = 12)

它具有以下结构:

‘zooreg’ series from Jun 1993 to Dec 2017
  Data: num [1:295, 1] 16.4 15.7 16.4 16.2 15.6 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:295] "1993-06-30" "1993-07-07" "1993-08-30" "1993-09-30" ...
  ..$ : chr "AverageTemp"
  Index:  'yearmon' num [1:295] Jun 1993 Jul 1993 Aug 1993 Sep 1993 ...
  Frequency: 12 

它将最后日期更改为2017年12月。为什么会这样?如何解决该问题,使原始结束日期为2018年5月?

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

您想要as.zooreg,而不是zooregzooreg用于从其数据和索引构造Zooreg对象,而as.zooreg用于将其他对象转换为Zooreg类。 zooreg的第一个参数指定数据部分,其他参数指定索引部分。 zooreg的第一个参数应该是数字矢量或矩阵,但是由于给出了xts对象,因此它忽略了时间索引而占用了它的数据部分。

as.zooreg可用于从xts类转换为zooreg类。下面的第一行进行对话,保留Date类的索引,然后下一行使用yearmon类将索引转换为年/月。

zr <- as.zooreg(StreamsTempAveragextsMonthly)
zr <- aggregate(zr, as.yearmon, c) ##
range(index(zr))
## [1] "Jun 1993" "May 2018"

标记为##的行可以交替显示:

index(zr) <- as.yearmon(index(zr))