Conversion from xts to zooreg time series removes date values - r

I hope you are having a nice day!
First, here's the dput of my data:
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"))
It has this structure:
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
Notice how the last date is 2018-05-02? Now, the problem is when I convert my xts object into a zooreg object. I used this code:
StreamsTempzooreg <- zooreg(StreamsTempAveragextsMonthly, start = c(1993,6), end = c(2018,5), frequency = 12)
It has this structure:
‘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
It changes the last date to December 2017. Why is this happening? How can I fix it so that the original end date is May 2018?
Any help would be greatly appreciated. Thank you.

You want as.zooreg, not zooreg. zooreg is used to construct a zooreg object from its data and index whereas as.zooreg is used to convert other objects to zooreg class. The first argument of zooreg specifies the data portion and the other arguments specify the index portion. The first argument of zooreg should be a numeric vector or matrix but since an xts object was given instead it took the data portion of it ignoring the time index.
as.zooreg can be used to convert from xts class to zooreg class. The first line below does the converstion keeping the Date class index and then the next line converts the index to year/month using the yearmon class.
zr <- as.zooreg(StreamsTempAveragextsMonthly)
zr <- aggregate(zr, as.yearmon, c) ##
range(index(zr))
## [1] "Jun 1993" "May 2018"
The line marked ## could alternatley be written:
index(zr) <- as.yearmon(index(zr))

Related

extract item from list in R using tidyverse

I have a list in R from a Structural VAR Model I've run. Here is the dataset I've used
structure(c(-2.46773175636719, -7.72826473957142, 11.7280053716074,
-1.90017613519231, 2.46070753200911, -10.9055849254775, 12.5325444143789,
1.3401777880631, 1.11706357603545, -11.9683333308867, 13.8014223673123,
3.00567366940339, 1.93467425287253, -12.4811258520841, 12.5337510851622,
6.17970387794458, -0.311524449552714, -4.43067381564184, 6.00580266302142,
0.733215859597713, -0.0920826461465296, -10.4968848655989, 15.1592141225619,
-0.0142294828419764, 2.81612947228318, -7.95484787976672, 11.3258100085652,
-4.26536944046738, 1.71777230300645, 3.05071906367438, 6.86888493397788,
-2.04121390648186, -5.45632423113958, 4.32701496742318, -1.77039007913358,
-3.38387552001187, -3.6948032720689, 9.71137617961375, 7.70239243534423,
-7.34908250395865, 1.66529724744393, 4.12699586248877, 1.46757481960265,
0.371903496562886, -3.1265641490501, 10.1504446414976, -6.97113134346488,
10.6335568680931, -7.74296970668011, -0.31563625249369, 4.86848083529221,
0.0394209612387897, 4.1060141554107, 4.40828962229318, -3.65631265465627,
-0.804371886780864, 5.300399767623, 3.33547294572138, 2.19966779280814,
-2.96620054964727, 2.01147630916658, 3.00808531323007, -1.78858517036105,
-0.262917554632125, -2.63540419004151, 3.42060573075127, -0.379197105963414,
-2.24861964821645, -1.33132752566709, 1.67672890048411, 2.42834777679839,
-1.28230298674303, 2.33228006495114, -1.79196678955762, -3.25533945043563,
-2.49506277883942, 3.87275419034108, -0.237088301198796, 3.15158720918571,
-8.16796902280537, -5.40342750457601, -0.684253285161596, 2.76776077124627,
3.03762540403962, 1.59655060303945, 2.81108799665279, 2.12619249528281,
1.25976266808472, 1.75072385830202, 3.35523567403357, 3.66056933213099,
2.81157744760496, 2.8694753969682, 1.22389693906415, 1.23949528512668,
0.655297291930701, -0.0811557166513488, 2.49469766016768, 1.04750905161959,
0.553154355370644, 0.0394037933455493, 0.495095459931427, 0.263970407073533,
1.46876270708987, 1.02690822562028, 1.21249150827216, 0.744350196120713,
2.28082505446663, 1.40375659950536, 1.59077468759525, 1.74048892256584,
1.81791859189397, 0.640927792895951, 2.05320969806211, 3.31708856718933,
3.94172305193239, 1.29907384513261, 2.18844272980503, 2.02564945461834,
2.89662683480758, 0.531971587307556, 1.69103059033393, 0.195326999327161,
1.20287931837835, -0.0417495586606087, 1.89055607207678, 1.8793164132429,
1.19212718508228, 1.43097285231573, 2.4079363839828, 1.02287700043888,
1.41700117422383, 1.9324642470476, 1.42192313279805, 1.00916357593048,
1.26198292983659, 0.929701659038162, 1.77329986790085, 1.88793046797189,
0.685737107435092, 0.400744609924519, 0.766302538581343, 1.09780031685389,
1.00049427220021, 0.535284014354875, 3.18688973234602, 1.75770144393539,
1.19538307882463, 0.904511107610428, 3.52708500618766, 0.526934426385495,
0.361429093322574, 0.624469028588326, 1.95600784906818, 0.781933189336748,
1.09121935535335, 1.30962848646536, 1.24173607356797, 0.453213216209214,
0.468243562258674, 0.489076348335971, 0.848706378173603, 0.125371629239357,
0.817153292286932), .Dim = c(82L, 2L), .Dimnames = list(NULL,
c("GDP_NAM", "CPI_NAM")), index = structure(c(962323200,
970272000, 978220800, 985996800, 993859200, 1001808000, 1009756800,
1017532800, 1025395200, 1033344000, 1041292800, 1049068800, 1056931200,
1064880000, 1072828800, 1080691200, 1088553600, 1096502400, 1104451200,
1112227200, 1120089600, 1128038400, 1135987200, 1143763200, 1151625600,
1159574400, 1167523200, 1175299200, 1183161600, 1191110400, 1199059200,
1206921600, 1214784000, 1222732800, 1230681600, 1238457600, 1246320000,
1254268800, 1262217600, 1269993600, 1277856000, 1285804800, 1293753600,
1301529600, 1309392000, 1317340800, 1325289600, 1333152000, 1341014400,
1348963200, 1356912000, 1364688000, 1372550400, 1380499200, 1388448000,
1396224000, 1404086400, 1412035200, 1419984000, 1427760000, 1435622400,
1443571200, 1451520000, 1459382400, 1467244800, 1475193600, 1483142400,
1490918400, 1498780800, 1506729600, 1514678400, 1522454400, 1530316800,
1538265600, 1546214400, 1553990400, 1561852800, 1569801600, 1577750400,
1585612800, 1593475200, 1601424000), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo"))
and the code I've used to create the model
library(tidyverse)
library(vars)
var.namibia <- namibia %>% VAR(.,p=1,type = 'both',season=NULL)
SVAR.namibia <- BQ(var.namibia)
from this, I want to extract residuals from the variables in my model. I can do it by indexing (see below), but I'm interested in finding a dplyr solution to achieve the same. Any suggestions?
res <- SVAR.namibia$var$varresult$CPI_NAM$residuals
You can use purrr's pluck function.
SVAR.namibia %>%
purrr::pluck('var', 'varresult', 'CPI_NAM', 'residuals')

Getting date to appear on x axis of plot.stl method in R

I was wondering if anyone knows of a way to get the dates to appear on the x axis of an stl plot.
res<- (stl(ts(data[,variable],frequency=52,start=as.Date(data[1,date]) ),s.window="per",robust=TRUE) )
plot(res)
However this is just producing some unicode labels at the bottom of the chart, I have also tried chaning as.Date to as.character however this didn't work.
dput of some data:
structure(c("2007-01-01", "2007-01-08", "2007-01-15", "2007-01-22",
"2007-01-29", "2007-02-05", "2007-02-12", "2007-02-19", "2007-02-26",
"2007-03-05", "2007-03-12", "2007-03-19", "2007-03-26", "2007-04-02",
"2007-04-09", "2007-04-16", "2007-04-23", "2007-04-30", "2007-05-07",
"2007-05-14", "2007-05-21", "2007-05-28", "2007-06-04", "2007-06-11",
"2007-06-18", "2007-06-25", "2007-07-02", "2007-07-09", "2007-07-16",
"2007-07-23", "2007-07-30", "2007-08-06", "2007-08-13", "2007-08-20",
"2007-08-27", "2007-09-03", "2007-09-10", "2007-09-17", "2007-09-24",
"2007-10-01", "2007-10-08", "2007-10-15", "2007-10-22", "2007-10-29",
"2007-11-05", "2007-11-12", "2007-11-19", "2007-11-26", "2007-12-03",
"2007-12-10", "2007-12-17", "2007-12-24", "2007-12-31", "2008-01-07",
"2008-01-14", "2008-01-21", "2008-01-28", "2008-02-04", "2008-02-11",
"2008-02-18", "2008-02-25", "2008-03-03", "2008-03-10", "2008-03-17",
"2008-03-24", "2008-03-31", "2008-04-07", "2008-04-14", "2008-04-21",
"2008-04-28", "2008-05-05", "2008-05-12", "2008-05-19", "2008-05-26",
"2008-06-02", "2008-06-09", "2008-06-16", "2008-06-23", "2008-06-30",
"2008-07-07", "2008-07-14", "2008-07-21", "2008-07-28", "2008-08-04",
"2008-08-11", "2008-08-18", "2008-08-25", "2008-09-01", "2008-09-08",
"2008-09-15", "2008-09-22", "2008-09-29", "2008-10-06", "2008-10-13",
"2008-10-20", "2008-10-27", "2008-11-03", "2008-11-10", "2008-11-17",
"2008-11-24", "2008-12-01", "2008-12-08", "2008-12-15", "2008-12-22",
"2008-12-29", "2009-01-05", "2009-01-12", "2009-01-19", "2009-01-26",
"2009-02-02", "2009-02-09", "2009-02-16", "2009-02-23", "2009-03-02",
"2009-03-09", "2009-03-16", "2009-03-23", "2009-03-30", "2009-04-06",
"2009-04-13", "2009-04-20", "2009-04-27", "2009-05-04", "2009-05-11",
"2009-05-18", "2009-05-25", "2009-06-01", "2009-06-08", "2009-06-15",
"2009-06-22", "2009-06-29", "2009-07-06", "2009-07-13", "2009-07-20",
"2009-07-27", "2009-08-03", "2009-08-10", "2009-08-17", "2009-08-24",
"2009-08-31", "2009-09-07", "2009-09-14", "2009-09-21", "2009-09-28",
"2009-10-05", "2009-10-12", "2009-10-19", "2009-10-26", "2009-11-02",
"2009-11-09", "2009-11-16", "2009-11-23", "2009-11-30", "2009-12-07",
"2009-12-14", "2009-12-21", "2009-12-28", "2010-01-04", "2010-01-11",
"2010-01-18", "2010-01-25", "2010-02-01", "2010-02-08", "2010-02-15",
"2010-02-22", "2010-03-01", "2010-03-08", "2010-03-15", "2010-03-22",
"2010-03-29", "2010-04-05", "2010-04-12", "2010-04-19", "2010-04-26",
"2010-05-03", "2010-05-10", "2010-05-17", "2010-05-24", "2010-05-31",
"2010-06-07", "2010-06-14", "2010-06-21", "2010-06-28", "2010-07-05",
"2010-07-12", "2010-07-19", "2010-07-26", "2010-08-02", "2010-08-09",
"2010-08-16", "2010-08-23", "2010-08-30", "2010-09-06", "2010-09-13",
"2010-09-20", "2010-09-27", "2010-10-04", "2010-10-11", "2010-10-18",
"2010-10-25", "2010-11-01", "2010-11-08", "2010-11-15", "2010-11-22",
"2010-11-29", "2010-12-06", "2010-12-13", "2010-12-20", "2010-12-27",
"2011-01-03", "2011-01-10", "2011-01-17", "2011-01-24", "2011-01-31",
"2011-02-07", "2011-02-14", "2011-02-21", "2011-02-28", "2011-03-07",
"2011-03-14", "2011-03-21", "2011-03-28", "2011-04-04", "2011-04-11",
"2011-04-18", "2011-04-25", "2011-05-02", "2011-05-09", "2011-05-16",
"2011-05-23", "2011-05-30", "2011-06-06", "2011-06-13", "2011-06-20",
"2011-06-27", "2011-07-04", "2011-07-11", "2011-07-18", "2011-07-25",
"2011-08-01", "2011-08-08", "2011-08-15", "2011-08-22", "2011-08-29",
"2011-09-05", "2011-09-12", "2011-09-19", "2011-09-26", "2011-10-03",
"2011-10-10", "2011-10-17", "2011-10-24", "2011-10-31", "2011-11-07",
"2011-11-14", "2011-11-21", "2011-11-28", "2011-12-05", "2011-12-12",
"2011-12-19", "2011-12-26", "442573", "452832", "452785", "459228",
"479509", "477631", "465619", "462001", "485567", "462381", "456059",
"457401", "474094", "468766", "456945", "539126", "545640", "511801",
"486619", "484430", "481428", "470622", "479677", "486755", "477617",
"483656", "497479", "493436", "480080", "481527", "516029", "532349",
"503939", "472171", "461550", "504532", "489715", "480032", "488376",
"470772", "467395", "488155", "455712", "474456", "471237", "482943",
"459320", "456956", "465056", "461930", "441201", "451255", "464508",
"449199", "455411", "476323", "521761", "513416", "521070", "497596",
"485461", "485593", "461148", "429938", "441207", "459484", "462099",
"469285", "454395", "456729", "469251", "517727", "526719", "477985",
"484538", "469766", "472399", "481162", "479039", "481071", "485068",
"462108", "459079", "452410", "488574", "502166", "504323", "514529",
"524206", "504315", "462935", "461899", "461551", "455491", "456857",
"443314", "454586", "458943", "450555", "454311", "442808", "440126",
"414876", "415787", "413352", "423864", "448319", "415440", "431948",
"433313", "448509", "436400", "454154", "454183", "447735", "452220",
"451433", "472808", "446767", "426595", "463693", "435673", "452704",
"456828", "465069", "448685", "457353", "443859", "463972", "480139",
"493342", "500982", "529602", "527365", "512005", "484585", "455007",
"470006", "477522", "443956", "459038", "488877", "466669", "476242",
"470862", "457298", "466438", "454062", "460216", "466645", "445113",
"457255", "451553", "451504", "447991", "435100", "416140", "481390",
"489041", "496984", "486245", "478191", "455201", "448085", "459097",
"480859", "490248", "462523", "489755", "468391", "460229", "481276",
"472845", "481099", "476435", "487314", "475043", "476847", "461928",
"483488", "479379", "456732", "461538", "480773", "471101", "459898",
"482129", "464356", "459420", "457850", "456764", "438152", "467928",
"464732", "458767", "470256", "449488", "433263", "428548", "435099",
"429163", "424251", "434723", "425841", "399498", "418791", "405051",
"385037", "425144", "418296", "397644", "414283", "431907", "429117",
"424862", "439664", "432791", "443588", "434985", "442418", "445760",
"449290", "451412", "456247", "444372", "441390", "458192", "456435",
"450670", "447609", "439083", "464513", "462784", "439423", "450857",
"442374", "447753", "440207", "435254", "430841", "437233", "426523",
"430127", "431305", "470244", "508878", "511064", "504182", "462076",
"452218", "426535", "436892", "459008", "441449", "438783", "427497",
"432275", "436745", "423068", "429574", "416074"), .Dim = c(261L,
2L))
The problem is with how you have created the time series object.
?ts will give you
start : the time of the first observation. Either a single number or a
vector of two integers, which specify a natural time unit and a
(1-based) number of samples into the time unit.
Since your data starts at 2007-01-01 and is a weekly data, you have to specify c(2007,1), Note that the "1" here denotes that data start from the 1st week of 2007, as the data is weekly. Here the frequency should be set to 52 because, after 52 cycles/weeks a year will change from 2007 to 2008.
This should work
res<- (stl(ts(data[,2],frequency=52,start=c(2007,1 )),s.window="per",robust=TRUE) )

R - Color or shade area between lines

I'm trying to replicate with R a chart I made on Excel, which should represent a 95% Confidence Interval (CI) around a time series forecast. The Excel chart looks like this:
So, basically, the original historical time series and from a certain point in time the forecast of what it could be with its respective CI.
They way it's done on Excel is a bit inefficient:
I have four time series which overlap much of the time;
The actual/historical time series (blue line above) simply stops when the forecast begins;
The forecast (dotted red above) is simply hidden below the blue one until the forecast period begins;
Then I have a time series representing the difference between the the upper bound and the lower bound of the CI, which playing around with Excel Stacked Areas charts, becomes the shaded area in the chart above.
Obviously, the computation to generate the forecast and the CIs is much faster and easier to generalize and use with R, and while I could complete the task on R and then simply copy the output on Excel to draw the chart, doing everything in R would be much nicer.
At the end of the question I provided the raw data with dput() as suggested by #MLavoie.
Here the packages I loaded (not sure you need them all here, but they are the ones I usually work with):
require(zoo)
require(xts)
require(lattice)
require(latticeExtra)
My data looks like this for the first 100 rows:
> head(data)
fifth_percentile Median nintyfifth_percentile
2017-06-18 1.146267 1.146267 1.146267
2017-06-19 1.134643 1.134643 1.134643
2017-06-20 1.125664 1.125664 1.125664
2017-06-21 1.129037 1.129037 1.129037
2017-06-22 1.147542 1.147542 1.147542
2017-06-23 1.159989 1.159989 1.159989
Then after the 100 data point, the time series start to diverge and at the end they look like this:
> tail(data)
fifth_percentile Median nintyfifth_percentile
2017-12-30 0.9430930 1.125844 1.341603
2017-12-31 0.9435227 1.127391 1.354928
2018-01-01 0.9417235 1.124625 1.355527
2018-01-02 0.9470077 1.124088 1.361420
2018-01-03 0.9571596 1.127299 1.364005
2018-01-04 0.9515535 1.127978 1.369536
Solution provided by DaveTurek
Thanks to DaveTurek I've found the answer. However, only difference is that for my xts dataframe, apparently, I need first to convert each column to numbers (with as.numeric()). No idea if that stems from me doing something wrong with xts and lattice, or it is the only way to achieve it using DaveTurek suggestion. Will try to investigate it further.
Here is the code to generate the chart:
x = index(data[1:100,2])
y = as.numeric(data[1:100,2])
ex.x = index(data[101:200,2])
ex.y = as.numeric(data[101:200,2])
ex.lo = as.numeric(data[101:200,1])
ex.hi = as.numeric(data[101:200,3])
xyplot(y~x, ylim = c(0.9,1.4),
panel=function(x,y,...) {
panel.lines(x,y,lwd=2,col=4)
panel.polygon(c(ex.x,rev(ex.x)),c(ex.lo,rev(ex.hi)),border=NA,col=5)
panel.lines(ex.x,ex.y,lwd=2,col=2)
})
And here the final result:
Here is the final dataset, from dput(), that I'm trying to plot:
> dput(data)
structure(c(1.14626724930899, 1.13464279067717, 1.12566420479952,
1.12903662366847, 1.14754211999921, 1.15998855701439, 1.15274364578958,
1.16226441955745, 1.16169992687419, 1.16520028734587, 1.16823402018407,
1.19832130049664, 1.18411773220697, 1.18531274215286, 1.16421444455115,
1.17108139956539, 1.18392357740377, 1.20103911352579, 1.17791736605905,
1.18277944964829, 1.20162550199013, 1.19665058179752, 1.19411188122108,
1.19367558590966, 1.19803272562951, 1.20600155861871, 1.22189449901607,
1.22072774140118, 1.22312376195254, 1.25355505518571, 1.25895911759195,
1.2613354420716, 1.24440525381363, 1.24444079462029, 1.24168652168112,
1.24154936710117, 1.23440527301777, 1.22592718438811, 1.21709102449773,
1.21448030929365, 1.23109601090898, 1.24401127451953, 1.23953314346685,
1.21863565024168, 1.20834325548551, 1.20281193695583, 1.20405850724191,
1.19608032796923, 1.22008184095742, 1.21675995421116, 1.20198916403093,
1.20029121301547, 1.18822375424598, 1.19007923345344, 1.19285965857709,
1.1971013197471, 1.1776860331227, 1.18028531916998, 1.18394951589397,
1.16712430930941, 1.17827461393349, 1.18751430033172, 1.21482260909863,
1.2167262724184, 1.21729489152574, 1.21847062594996, 1.21932070698031,
1.19678189566773, 1.17678214957629, 1.17586968485613, 1.16903708967946,
1.16967697995898, 1.14498266161799, 1.12782282645368, 1.11540004479973,
1.12639853863918, 1.11402516325222, 1.10511837662567, 1.10600107687395,
1.10243149863659, 1.10404564773364, 1.12949458422398, 1.11679224666313,
1.11338078540871, 1.10762728498848, 1.12437898939299, 1.11572706259347,
1.1148111967932, 1.12358625045939, 1.11169207274881, 1.13009253108247,
1.13772927166761, 1.12550770863279, 1.13062401691547, 1.12821231512428,
1.13174620070443, 1.13072790983063, 1.1428325334377, 1.12739171867048,
1.1214997813059, 1.11870510839984, 1.096148222775, 1.08805136310032,
1.08701594286129, 1.08047984136855, 1.07939438148434, 1.0684082570972,
1.06497159411023, 1.05820047926833, 1.06322519359802, 1.06234781015662,
1.05431808916504, 1.054405104791, 1.05330182895869, 1.04787681441803,
1.041698698458, 1.03870702538097, 1.03300007904201, 1.02741553353049,
1.03525701392318, 1.0339774223954, 1.0328464056954, 1.03100871401712,
1.03348765946373, 1.03473218333386, 1.02942612874379, 1.02109481188296,
1.02301597272716, 1.01553904377803, 1.0031650628692, 1.00779708136199,
1.01322764666693, 1.01964272925677, 1.02125480865504, 1.02300342204156,
1.02563993245866, 1.02972111884963, 1.02048756192688, 1.00481457379443,
1.00512607721887, 1.01094340128446, 1.01377432300649, 1.01170553705668,
1.00551128145228, 1.00612634442438, 1.00735643866839, 1.0080606590012,
0.985706701720841, 0.982234200010558, 0.975314534071082, 0.973611418201841,
0.968118612511537, 0.973092829667201, 0.975599110408158, 0.967214930243667,
0.968569928969912, 0.963572085616274, 0.964901787179726, 0.957782708788541,
0.951868416101986, 0.956694066411684, 0.956937537219092, 0.956303331651844,
0.947880835881923, 0.956308493824626, 0.948146077843001, 0.945939091828748,
0.945082701640947, 0.937222489932819, 0.937989843132858, 0.948712728941467,
0.939050882255992, 0.946264846068344, 0.944926693194716, 0.946825914432391,
0.939070104432721, 0.950666108330947, 0.949365988007735, 0.943616625744159,
0.946600795357699, 0.941276090147603, 0.939957902451166, 0.941523527816784,
0.946611480333791, 0.959236316317354, 0.96165367272139, 0.957508302724503,
0.954774123925477, 0.960811125123549, 0.956525507301749, 0.948237690612711,
0.951299123137395, 0.945212566792479, 0.94507842203255, 0.942735006048921,
0.943093032220433, 0.943522672031737, 0.941723495992432, 0.947007713852018,
0.95715960245335, 0.951553478810637, 1.14626724930899, 1.13464279067717,
1.12566420479952, 1.12903662366847, 1.14754211999921, 1.15998855701439,
1.15274364578958, 1.16226441955745, 1.16169992687419, 1.16520028734587,
1.16823402018407, 1.19832130049664, 1.18411773220697, 1.18531274215286,
1.16421444455115, 1.17108139956539, 1.18392357740377, 1.20103911352579,
1.17791736605905, 1.18277944964829, 1.20162550199013, 1.19665058179752,
1.19411188122108, 1.19367558590966, 1.19803272562951, 1.20600155861871,
1.22189449901607, 1.22072774140118, 1.22312376195254, 1.25355505518571,
1.25895911759195, 1.2613354420716, 1.24440525381363, 1.24444079462029,
1.24168652168112, 1.24154936710117, 1.23440527301777, 1.22592718438811,
1.21709102449773, 1.21448030929365, 1.23109601090898, 1.24401127451953,
1.23953314346685, 1.21863565024168, 1.20834325548551, 1.20281193695583,
1.20405850724191, 1.19608032796923, 1.22008184095742, 1.21675995421116,
1.20198916403093, 1.20029121301547, 1.18822375424598, 1.19007923345344,
1.19285965857709, 1.1971013197471, 1.1776860331227, 1.18028531916998,
1.18394951589397, 1.16712430930941, 1.17827461393349, 1.18751430033172,
1.21482260909863, 1.2167262724184, 1.21729489152574, 1.21847062594996,
1.21932070698031, 1.19678189566773, 1.17678214957629, 1.17586968485613,
1.16903708967946, 1.16967697995898, 1.14498266161799, 1.12782282645368,
1.11540004479973, 1.12639853863918, 1.11402516325222, 1.10511837662567,
1.10600107687395, 1.10243149863659, 1.10404564773364, 1.12949458422398,
1.11679224666313, 1.11338078540871, 1.10762728498848, 1.12437898939299,
1.11572706259347, 1.1148111967932, 1.12358625045939, 1.11169207274881,
1.13009253108247, 1.13772927166761, 1.12550770863279, 1.13062401691547,
1.12821231512428, 1.13174620070443, 1.13072790983063, 1.1428325334377,
1.12739171867048, 1.1214997813059, 1.11870510839984, 1.11811303551412,
1.11855383782522, 1.11981261957516, 1.12096887905804, 1.12162710713999,
1.12015553029278, 1.12189306008921, 1.1236834173899, 1.12204149206779,
1.12075809542535, 1.12116672935174, 1.12216772364685, 1.11821915571021,
1.12117719223463, 1.11896003906963, 1.11563621625852, 1.1183625095638,
1.12053072892388, 1.1216348268255, 1.12317377733957, 1.11873136428952,
1.12267083202989, 1.12642930089215, 1.13027646770951, 1.13129632891931,
1.12700346009603, 1.12060488827701, 1.12390899402613, 1.13129350591169,
1.12786650327192, 1.1274201121913, 1.13101906643359, 1.12727135093377,
1.12458327192256, 1.12259738972645, 1.12097982776572, 1.12073621452193,
1.12364872830763, 1.12644326299714, 1.12556263098661, 1.12797963752343,
1.12734519199847, 1.1261793072762, 1.12911407446825, 1.12754878937943,
1.12777579027467, 1.12554965831588, 1.12324469267853, 1.12231558194992,
1.12135908710208, 1.11923353817423, 1.12345300992675, 1.12186883237389,
1.12173652640663, 1.12488148969114, 1.12664301925369, 1.12294230775256,
1.12393650688095, 1.13038044949978, 1.12822226676967, 1.12934384230215,
1.1217648908055, 1.12218158739803, 1.12302651609468, 1.12682187689922,
1.13537701046932, 1.13172108462183, 1.1374053505525, 1.13498257452656,
1.12692005654471, 1.13210629725645, 1.12868775509168, 1.13073909215368,
1.13098804355869, 1.13353301668386, 1.13336476594698, 1.13233873705211,
1.12667020676157, 1.12133152301322, 1.12418759586717, 1.12048022460741,
1.12798162212357, 1.13053093896994, 1.12019367019997, 1.12422483586498,
1.11303086301782, 1.11986711815552, 1.12504718249418, 1.11341517044014,
1.12495096618792, 1.12995127061511, 1.13538401552385, 1.13145536081928,
1.1264465959783, 1.12584386458867, 1.1273908895838, 1.12462482614994,
1.1240880626286, 1.12729907535003, 1.12797751377714, 1.14626724930899,
1.13464279067717, 1.12566420479952, 1.12903662366847, 1.14754211999921,
1.15998855701439, 1.15274364578958, 1.16226441955745, 1.16169992687419,
1.16520028734587, 1.16823402018407, 1.19832130049664, 1.18411773220697,
1.18531274215286, 1.16421444455115, 1.17108139956539, 1.18392357740377,
1.20103911352579, 1.17791736605905, 1.18277944964829, 1.20162550199013,
1.19665058179752, 1.19411188122108, 1.19367558590966, 1.19803272562951,
1.20600155861871, 1.22189449901607, 1.22072774140118, 1.22312376195254,
1.25355505518571, 1.25895911759195, 1.2613354420716, 1.24440525381363,
1.24444079462029, 1.24168652168112, 1.24154936710117, 1.23440527301777,
1.22592718438811, 1.21709102449773, 1.21448030929365, 1.23109601090898,
1.24401127451953, 1.23953314346685, 1.21863565024168, 1.20834325548551,
1.20281193695583, 1.20405850724191, 1.19608032796923, 1.22008184095742,
1.21675995421116, 1.20198916403093, 1.20029121301547, 1.18822375424598,
1.19007923345344, 1.19285965857709, 1.1971013197471, 1.1776860331227,
1.18028531916998, 1.18394951589397, 1.16712430930941, 1.17827461393349,
1.18751430033172, 1.21482260909863, 1.2167262724184, 1.21729489152574,
1.21847062594996, 1.21932070698031, 1.19678189566773, 1.17678214957629,
1.17586968485613, 1.16903708967946, 1.16967697995898, 1.14498266161799,
1.12782282645368, 1.11540004479973, 1.12639853863918, 1.11402516325222,
1.10511837662567, 1.10600107687395, 1.10243149863659, 1.10404564773364,
1.12949458422398, 1.11679224666313, 1.11338078540871, 1.10762728498848,
1.12437898939299, 1.11572706259347, 1.1148111967932, 1.12358625045939,
1.11169207274881, 1.13009253108247, 1.13772927166761, 1.12550770863279,
1.13062401691547, 1.12821231512428, 1.13174620070443, 1.13072790983063,
1.1428325334377, 1.12739171867048, 1.1214997813059, 1.11870510839984,
1.14162401974592, 1.15630966411729, 1.15992199767135, 1.16683144867851,
1.16928280999155, 1.17287782220285, 1.18184525262982, 1.17555305757354,
1.18031492211593, 1.18142628277888, 1.18307577052783, 1.18257404220722,
1.19421117710041, 1.19403330560815, 1.19510080390052, 1.2058940348108,
1.19848571699109, 1.20138771250604, 1.20660682710938, 1.20790011589089,
1.20963951875753, 1.21572259411602, 1.21379678812156, 1.220302087399,
1.22062959185172, 1.22743877731977, 1.23135277550334, 1.24075667733246,
1.24169498945046, 1.23529301399753, 1.2399941777708, 1.24823732280171,
1.23861121958778, 1.24816319854615, 1.25252933549084, 1.25133386983018,
1.24512546001264, 1.2617641352045, 1.25486018976211, 1.25424601859098,
1.25820538036104, 1.25968528498312, 1.26939611029084, 1.27883933177157,
1.27926882841012, 1.27951234203094, 1.28997494816278, 1.29391898267335,
1.2971442938215, 1.29733541086814, 1.30376525837809, 1.31025722802128,
1.29718190520268, 1.27919305871102, 1.28685138548374, 1.28594279969497,
1.28695233433419, 1.30277136510213, 1.29178316107299, 1.29586799884087,
1.30076586308517, 1.30881154838964, 1.32171887794143, 1.3197588324899,
1.3121332301804, 1.31744410759858, 1.31402945919721, 1.30926303329755,
1.32019231597949, 1.31449633135152, 1.31730801686101, 1.31834557852015,
1.3175761022299, 1.33430488507454, 1.34091614601639, 1.33606628597812,
1.33180446732765, 1.33630738683041, 1.33449101077219, 1.32521028784732,
1.32241490851887, 1.31488015995544, 1.31913131799656, 1.32901121011698,
1.33177659436063, 1.32577077582349, 1.31960627618725, 1.31307169067904,
1.32148403094167, 1.33104893196281, 1.33491831741272, 1.3386091981919,
1.35730874062825, 1.3460340606746, 1.34160318929376, 1.35492848895938,
1.35552729646417, 1.36141957863605, 1.36400538435282, 1.369536167295),
.indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC",
class = c("xts", "zoo"), index = structure(c(1497744000, 1497830400, 1497916800,
1498003200, 1498089600, 1498176000, 1498262400, 1498348800, 1498435200,
1498521600, 1498608000, 1498694400, 1498780800, 1498867200, 1498953600,
1499040000, 1499126400, 1499212800, 1499299200, 1499385600, 1499472000,
1499558400, 1499644800, 1499731200, 1499817600, 1499904000, 1499990400,
1500076800, 1500163200, 1500249600, 1500336000, 1500422400, 1500508800,
1500595200, 1500681600, 1500768000, 1500854400, 1500940800, 1501027200,
1501113600, 1501200000, 1501286400, 1501372800, 1501459200, 1501545600,
1501632000, 1501718400, 1501804800, 1501891200, 1501977600, 1502064000,
1502150400, 1502236800, 1502323200, 1502409600, 1502496000, 1502582400,
1502668800, 1502755200, 1502841600, 1502928000, 1503014400, 1503100800,
1503187200, 1503273600, 1503360000, 1503446400, 1503532800, 1503619200,
1503705600, 1503792000, 1503878400, 1503964800, 1504051200, 1504137600,
1504224000, 1504310400, 1504396800, 1504483200, 1504569600, 1504656000,
1504742400, 1504828800, 1504915200, 1505001600, 1505088000, 1505174400,
1505260800, 1505347200, 1505433600, 1505520000, 1505606400, 1505692800,
1505779200, 1505865600, 1505952000, 1506038400, 1506124800, 1506211200,
1506297600, 1506384000, 1506470400, 1506556800, 1506643200, 1506729600,
1506816000, 1506902400, 1506988800, 1507075200, 1507161600, 1507248000,
1507334400, 1507420800, 1507507200, 1507593600, 1507680000, 1507766400,
1507852800, 1507939200, 1508025600, 1508112000, 1508198400, 1508284800,
1508371200, 1508457600, 1508544000, 1508630400, 1508716800, 1508803200,
1508889600, 1508976000, 1509062400, 1509148800, 1509235200, 1509321600,
1509408000, 1509494400, 1509580800, 1509667200, 1509753600, 1509840000,
1509926400, 1510012800, 1510099200, 1510185600, 1510272000, 1510358400,
1510444800, 1510531200, 1510617600, 1510704000, 1510790400, 1510876800,
1510963200, 1511049600, 1511136000, 1511222400, 1511308800, 1511395200,
1511481600, 1511568000, 1511654400, 1511740800, 1511827200, 1511913600,
1.512e+09, 1512086400, 1512172800, 1512259200, 1512345600, 1512432000,
1512518400, 1512604800, 1512691200, 1512777600, 1512864000, 1512950400,
1513036800, 1513123200, 1513209600, 1513296000, 1513382400, 1513468800,
1513555200, 1513641600, 1513728000, 1513814400, 1513900800, 1513987200,
1514073600, 1514160000, 1514246400, 1514332800, 1514419200, 1514505600,
1514592000, 1514678400, 1514764800, 1514851200, 1514937600, 1515024000
), tzone = "UTC", tclass = "Date"), .Dim = c(201L, 3L), .Dimnames = list(
NULL, c("fifth_percentile", "Median", "nintyfifth_percentile"
)))
I haven't tried with your data, but if the question is how to shade the forecast area, maybe this simple example will help.
library(lattice)
x = 1:12 # base data
y = x
ex.x = 12:16 # extrapolated data
ex.y = 12:16
ex.lo = 12+0:4*.3 # lower bound
ex.hi = 12+0:4*1.6 # upper bound
xyplot(y~x,xlim=c(0:18),ylim=c(0:20),
panel=function(x,y,...) {
panel.lines(x,y,lwd=2,col=4)
panel.polygon(c(ex.x,rev(ex.x)),c(ex.lo,rev(ex.hi)),border=NA,col=5)
panel.lines(ex.x,ex.y,lwd=2,col=2)
})
You can add the shaded polygon to the lattice plot in a panel function. I used c(ex.x,rev(ex.x)) and c(ex.lo,rev(ex.hi)) to construct the polygon boundary.

R: merge.xts and specific fill function

After merging two unequal time series I want to fill the blanks with a custom function. Lets say my Series1 is daily data and Series2 is monthly data. So now Series1 has for example 30 data points for one month and Series2 only one. If I make a left join Series 2 has 29 NAs which I don't like. Ideally I would like a fill function so that Series 2 takes always the previous value to fill these 29 days.
So for example if the 31. of January has a value of 10 and the 28th of February a value of 15, February 1-27. should have a value of 10 as well. Of course in the beginning this doesn't work (since the first row is probably also a NA), so the first row should take the value of the first row containing a value at all.
At the moment I have this, but still, all NAs are present:
Test<-merge.xts(Series1, Series2, join="left", fill=function(x) x[index(x)-1,])
Series1:
structure(c(1.51762156049755, 1.52103159497526, 1.51401262063846,
1.5226927459172, 1.52933295052158, 1.52409353403389, 1.52292452830189,
1.5268928035982, 1.53555449785816, 1.54004946727549, 1.54031650339111,
1.53987556561086, 1.53733857383492, 1.52781969068276, 1.5303624813154,
1.53149347601615, 1.53200449185851, 1.53034081463009, 1.52689961175818,
1.52616010353115, 1.52004035586536, 1.52604263206673, 1.53170366207736,
1.53332707472775, 1.5400318381871, 1.53717071341521, 1.53998696583186,
1.53676880222841, 1.53316818056702, 1.53512014787431, 1.54153071688263,
1.53692449355433, 1.53382906453686, 1.53159514756473, 1.5344496294263,
1.53717866027826, 1.53445133065986, 1.53503822351656, 1.5306399132321,
1.53633694255827, 1.53748747380887, 1.54019086070839, 1.54068532372772,
1.53600669892073, 1.53977166385926, 1.53468288606184, 1.53986928104575,
1.54024911693623, 1.5402127262549, 1.54151119402985, 1.53934776549289,
1.53958085476343, 1.53900838497995, 1.53818540787939, 1.53465613216017,
1.53500719942405, 1.53537650054565, 1.53317195624888, 1.53192246131958,
1.53136958262882, 1.53666845974538, 1.53503754022167, 1.53098678960901,
1.52377172091382, 1.52796773627915, 1.52584842623527, 1.52760075397182,
1.52793296089385, 1.52820374854273, 1.52947558770344, 1.52752869440459,
1.52590880810595, 1.51771286513362, 1.52378827099884, 1.52171596056488,
1.52387303280875, 1.52663662867745, 1.53114232706069, 1.52827140549273,
1.52923132443161, 1.52939594909482, 1.53232585173925, 1.53195117573147,
1.53853103261361, 1.53776866137519, 1.54085533920156, 1.5410640956972,
1.54313041923661, 1.54222657292872, 1.54302034987504, 1.54211182336182,
1.54181785998761, 1.5424089337942, 1.53578353604795, 1.53286652078775,
1.53120629370629, 1.53219713608012, 1.53192052980132, 1.53522245762712,
1.53543098889476, 1.53283647523016, 1.5296408481177, 1.52531916716648,
1.52295699845811, 1.52777060191165, 1.52890571231934, 1.5233980665583,
1.52386256533288, 1.51978021978022, 1.52140011865412, 1.51797040169133,
1.51707941929974, 1.52089868588385, 1.52408100748809, 1.52491920394625,
1.52068065032432, 1.52637418914305, 1.52848101265823, 1.52656088306313,
1.52858618908214, 1.53068778514246, 1.52826643894108, 1.52470085470085,
1.51927185710623, 1.52041166380789, 1.51975945017182, 1.52318452637941,
1.51831155433287, 1.51966908661151, 1.52143645470753, 1.52183128444256,
1.52286417239331, 1.52149627623561, 1.52065908330545, 1.51957958976098,
1.52554186145346, 1.52094733242134, 1.51794915836482, 1.51173708920188,
1.51222222222222, 1.5101414692347, 1.5068328319725, 1.50393081761006,
1.50417972831766, 1.50391986062718, 1.50638741635526, 1.50589880276151,
1.51000264387063, 1.50961116475029, 1.50934456435904, 1.50983477576711,
1.51314636283961, 1.50903004140604, 1.51011752231157, 1.50968426638366,
1.50718251520226, 1.50750460809269, 1.50457827082233, 1.50718301061836,
1.51371392834807, 1.51775147928994, 1.51589595375723, 1.51878256100905,
1.51964269437608, 1.52107244513819, 1.51828822238478, 1.51868515287852,
1.52112289685443, 1.52031478770132, 1.5218941402322, 1.51964269437608,
1.51789300712069, 1.51745137247773, 1.51548186148772, 1.51610254538819,
1.51619929213177, 1.51333333333333, 1.51241134751773, 1.51200286635614,
1.51837734821672, 1.5163433908046, 1.49981738495252, 1.50498640072529,
1.5011387446479, 1.49350888500138, 1.4836323284631, 1.48080845540515,
1.47762023908813, 1.47091566935708, 1.44464775846295, 1.46478356566398,
1.46516563624619, 1.47632234837995, 1.48080808080808, 1.47685016405396,
1.48288833837967, 1.48791693466875, 1.48385916780979, 1.48779368575624,
1.4842056932966, 1.48020986745214, 1.48406538215688, 1.48219003370684,
1.4840747090138, 1.48181569592562, 1.47840712792072, 1.48482921511628,
1.48070841239722, 1.47882236069719, 1.47693552738063, 1.47952903398448,
1.47818343722173, 1.48081910042028, 1.47554444841128, 1.47437042328987,
1.47387958352196, 1.46947082767978, 1.47113912651959, 1.47202166064982,
1.47102365047843, 1.47226211849192, 1.47248814529838, 1.46853962839961,
1.46421559878636, 1.46491463305623, 1.46394424090787, 1.47141221037794,
1.46876654314452, 1.46473285134897, 1.46621860629643, 1.45898901098901,
1.45649677590319, 1.4541381128097, 1.45816872969889, 1.46286215978929,
1.46461267605634, 1.46386925795053, 1.46151797603196, 1.46911608093717,
1.47140552169236, 1.4750490108715, 1.47230138938368, 1.47392733410322,
1.47497537827917, 1.47591916674085, 1.48151776966242, 1.47590146376294,
1.47583108715184, 1.47547136091502, 1.47256621169665, 1.47307171853857,
1.47527795353882, 1.47582605564059, 1.46818468184682, 1.46878890272097,
1.48522318688065, 1.48453427065026, 1.48568912373404, 1.4814585908529,
1.48118303373771, 1.47687244262587, 1.47909624621953, 1.48514136031072,
1.48368539325843, 1.47950599606229, 1.47334107350183, 1.47758127902822,
1.47985739750446, 1.48092011412268, 1.47403176869534, 1.48108736475007,
1.47305653710247, 1.46450017661604, 1.4681413589495, 1.46912050964431,
1.46845174973489, 1.47360950944735, 1.46758608573436, 1.46957056292263,
1.47418043421849, 1.47130794416681, 1.47095489568003, 1.47372954349699,
1.47756961155036, 1.47673216132368, 1.47682004001044, 1.47401301518438,
1.47194032439934, 1.47180647406892, 1.47518534670737, 1.47624474053296,
1.47794826830338, 1.48057829646403, 1.48357504805172, 1.48148471615721,
1.47989206128134, 1.47923238696109, 1.47960337479342, 1.47915397336583,
1.47995097180879, 1.47630640813842, 1.47675825125281, 1.47637181928337,
1.47504781777082, 1.47135191275749, 1.47813993915689, 1.47672594142259,
1.47480059602069, 1.47183284845279, 1.46386701662292, 1.47050586381936,
1.46995971273428, 1.46776454099509, 1.46059482834701, 1.45992231638418,
1.46362994350282, 1.4642195358687, 1.46497830514478, 1.46292372881356,
1.46326046879115, 1.46075594141892, 1.4626918018413, 1.46522991013001,
1.46767729569611, 1.46556834030683, 1.46354350123283, 1.46293202005101,
1.46216192405955, 1.46279539664412, 1.46416652028807, 1.46635751159332,
1.46744206538021, 1.46897280168999, 1.46536662843025, 1.46557031043884,
1.46789797713281, 1.46835554770942, 1.4694150120203, 1.46747460345749,
1.46710702490404, 1.46860547847741, 1.46663705019991, 1.4664345652562,
1.46345186781609, 1.46563852813853, 1.46283081925752, 1.45655110310671,
1.45227952506118, 1.45321531791908, 1.4547789396441, 1.45564738292011,
1.45421278931479, 1.45517865219358, 1.45266890970265, 1.45443743716296,
1.45503465888362, 1.45726148569365, 1.45540762356374, 1.45618509746766,
1.45435302779312, 1.45287885766928, 1.45133394664213, 1.45226409852764,
1.45390070921986, 1.45874769797422, 1.45672988399926, 1.46178846689572,
1.4674146797569, 1.4640179910045, 1.46468609865471, 1.47339173024395,
1.47045561296383, 1.47032863849765, 1.47437233538607, 1.47061043494669,
1.47251605591235, 1.47473215132265, 1.47768657420511, 1.47433962264151,
1.47615894039735, 1.47619047619048, 1.47506661591169, 1.47083612680778,
1.47052580800772, 1.4673786407767, 1.46719083673073, 1.46737852664577,
1.4680161147686, 1.47064637280095, 1.46837200079318, 1.47009818506397,
1.46631153201144, 1.46435925090695, 1.46418085731063, 1.46629705281587,
1.47042504706232, 1.47244016287615, 1.46962801741195, 1.46572500987752,
1.47040745514028, 1.46655971122029, 1.46671388101983, 1.46569960713206,
1.46444107233182, 1.45887708649469, 1.45496722138174, 1.4528824285573,
1.45116001194862, 1.4471463022508, 1.44598993785144, 1.45799803729146,
1.45748550083554, 1.45195033727637, 1.44973909618982, 1.44844597927972,
1.45353852185846, 1.45797913446677, 1.45808966861598, 1.46286266924565,
1.45828482731859, 1.4618320610687, 1.46203029706866, 1.46219309400372,
1.46284480219888, 1.46597735105859, 1.46784424709671, 1.4689243417833,
1.46860898567785, 1.47238907188529, 1.47246010120669, 1.47172011661808,
1.46688286163522, 1.46971327918583, 1.47072714749582, 1.47229862475442,
1.47179285222014, 1.46633416458853, 1.46399523903987, 1.46048587010412,
1.45797329143755, 1.45885579937304, 1.45979140267083, 1.46490971205466,
1.46888496270122, 1.46831875607386, 1.46836546846236, 1.46927047823123,
1.46807470421433, 1.462829499457, 1.46497003046084, 1.46442900479499,
1.46273932253314, 1.46406951767233, 1.4673116388156, 1.46543100912033,
1.45540647198106, 1.46271003242606, 1.45876085240726, 1.45973718012054,
1.46011549378487, 1.46333792018872, 1.46617056692451, 1.46380829785127,
1.4638067061144, 1.46371087192653, 1.46229022704837, 1.46666666666667,
1.4661108386464, 1.46767617938264, 1.46891393044492, 1.47142439879272,
1.46808094632906, 1.46796059689847, 1.46733815763739, 1.46692037470726,
1.4646265866378, 1.46480534801416, 1.46492177506642, 1.4623687858982,
1.46242774566474, 1.46307385229541, 1.4626074785043, 1.4633068968979,
1.46385298869144, 1.46180344478217, 1.46254927726675, 1.46241896272285,
1.46647171523646, 1.46721558389397, 1.46642431586388, 1.46720484359233,
1.46822373696872, 1.46890958245719, 1.46962101463806, 1.47268740031898,
1.47340742210756, 1.47341746993938, 1.47524262230145, 1.47560369671072,
1.47479367604653, 1.47198963317384, 1.47108097327483, 1.47302572315084,
1.4712827696618, 1.47083753784057, 1.47290739991913, 1.47313237221494,
1.47367359289893, 1.47733523479678, 1.47741935483871, 1.47505622572071,
1.46778337272634, 1.46253469010176, 1.46209942481512, 1.46357003391224,
1.45595482546201, 1.45030384179627, 1.45351356929109, 1.45500778412039,
1.44706984490476, 1.45556604763404, 1.45198866617693), .indexTZ = "UTC", .indexCLASS = "Date", tclass = "Date", tzone = "UTC", class = c("xts",
"zoo"), index = structure(c(978307200, 978393600, 978480000,
978566400, 978652800, 978912000, 978998400, 979084800, 979171200,
979257600, 979516800, 979603200, 979689600, 979776000, 979862400,
980121600, 980208000, 980294400, 980380800, 980467200, 980726400,
980812800, 980899200, 980985600, 981072000, 981331200, 981417600,
981504000, 981590400, 981676800, 981936000, 982022400, 982108800,
982195200, 982281600, 982540800, 982627200, 982713600, 982800000,
982886400, 983145600, 983232000, 983318400, 983404800, 983491200,
983750400, 983836800, 983923200, 984009600, 984096000, 984355200,
984441600, 984528000, 984614400, 984700800, 984960000, 985046400,
985132800, 985219200, 985305600, 985564800, 985651200, 985737600,
985824000, 985910400, 986169600, 986256000, 986342400, 986428800,
986515200, 986774400, 986860800, 986947200, 987033600, 987120000,
987379200, 987465600, 987552000, 987638400, 987724800, 987984000,
988070400, 988156800, 988243200, 988329600, 988588800, 988675200,
988761600, 988848000, 988934400, 989193600, 989280000, 989366400,
989452800, 989539200, 989798400, 989884800, 989971200, 990057600,
990144000, 990403200, 990489600, 990576000, 990662400, 990748800,
991008000, 991094400, 991180800, 991267200, 991353600, 991612800,
991699200, 991785600, 991872000, 991958400, 992217600, 992304000,
992390400, 992476800, 992563200, 992822400, 992908800, 992995200,
993081600, 993168000, 993427200, 993513600, 993600000, 993686400,
993772800, 994032000, 994118400, 994204800, 994291200, 994377600,
994636800, 994723200, 994809600, 994896000, 994982400, 995241600,
995328000, 995414400, 995500800, 995587200, 995846400, 995932800,
996019200, 996105600, 996192000, 996451200, 996537600, 996624000,
996710400, 996796800, 997056000, 997142400, 997228800, 997315200,
997401600, 997660800, 997747200, 997833600, 997920000, 998006400,
998265600, 998352000, 998438400, 998524800, 998611200, 998870400,
998956800, 999043200, 999129600, 999216000, 999475200, 999561600,
999648000, 999734400, 999820800, 1000080000, 1000166400, 1000252800,
1000339200, 1000425600, 1000684800, 1000771200, 1000857600, 1000944000,
1001030400, 1001289600, 1001376000, 1001462400, 1001548800, 1001635200,
1001894400, 1001980800, 1002067200, 1002153600, 1002240000, 1002499200,
1002585600, 1002672000, 1002758400, 1002844800, 1003104000, 1003190400,
1003276800, 1003363200, 1003449600, 1003708800, 1003795200, 1003881600,
1003968000, 1004054400, 1004313600, 1004400000, 1004486400, 1004572800,
1004659200, 1004918400, 1005004800, 1005091200, 1005177600, 1005264000,
1005523200, 1005609600, 1005696000, 1005782400, 1005868800, 1006128000,
1006214400, 1006300800, 1006387200, 1006473600, 1006732800, 1006819200,
1006905600, 1006992000, 1007078400, 1007337600, 1007424000, 1007510400,
1007596800, 1007683200, 1007942400, 1008028800, 1008115200, 1008201600,
1008288000, 1008547200, 1008633600, 1008720000, 1008806400, 1008892800,
1009152000, 1009238400, 1009324800, 1009411200, 1009497600, 1009756800,
1009843200, 1009929600, 1010016000, 1010102400, 1010361600, 1010448000,
1010534400, 1010620800, 1010707200, 1010966400, 1011052800, 1011139200,
1011225600, 1011312000, 1011571200, 1011657600, 1011744000, 1011830400,
1011916800, 1012176000, 1012262400, 1012348800, 1012435200, 1012521600,
1012780800, 1012867200, 1012953600, 1013040000, 1013126400, 1013385600,
1013472000, 1013558400, 1013644800, 1013731200, 1013990400, 1014076800,
1014163200, 1014249600, 1014336000, 1014595200, 1014681600, 1014768000,
1014854400, 1014940800, 1015200000, 1015286400, 1015372800, 1015459200,
1015545600, 1015804800, 1015891200, 1015977600, 1016064000, 1016150400,
1016409600, 1016496000, 1016582400, 1016668800, 1016755200, 1017014400,
1017100800, 1017187200, 1017273600, 1017360000, 1017619200, 1017705600,
1017792000, 1017878400, 1017964800, 1018224000, 1018310400, 1018396800,
1018483200, 1018569600, 1018828800, 1018915200, 1019001600, 1019088000,
1019174400, 1019433600, 1019520000, 1019606400, 1019692800, 1019779200,
1020038400, 1020124800, 1020211200, 1020297600, 1020384000, 1020643200,
1020729600, 1020816000, 1020902400, 1020988800, 1021248000, 1021334400,
1021420800, 1021507200, 1021593600, 1021852800, 1021939200, 1022025600,
1022112000, 1022198400, 1022457600, 1022544000, 1022630400, 1022716800,
1022803200, 1023062400, 1023148800, 1023235200, 1023321600, 1023408000,
1023667200, 1023753600, 1023840000, 1023926400, 1024012800, 1024272000,
1024358400, 1024444800, 1024531200, 1024617600, 1024876800, 1024963200,
1025049600, 1025136000, 1025222400, 1025481600, 1025568000, 1025654400,
1025740800, 1025827200, 1026086400, 1026172800, 1026259200, 1026345600,
1026432000, 1026691200, 1026777600, 1026864000, 1026950400, 1027036800,
1027296000, 1027382400, 1027468800, 1027555200, 1027641600, 1027900800,
1027987200, 1028073600, 1028160000, 1028246400, 1028505600, 1028592000,
1028678400, 1028764800, 1028851200, 1029110400, 1029196800, 1029283200,
1029369600, 1029456000, 1029715200, 1029801600, 1029888000, 1029974400,
1030060800, 1030320000, 1030406400, 1030492800, 1030579200, 1030665600,
1030924800, 1031011200, 1031097600, 1031184000, 1031270400, 1031529600,
1031616000, 1031702400, 1031788800, 1031875200, 1032134400, 1032220800,
1032307200, 1032393600, 1032480000, 1032739200, 1032825600, 1032912000,
1032998400, 1033084800, 1033344000, 1033430400, 1033516800, 1033603200,
1033689600, 1033948800, 1034035200, 1034121600, 1034208000, 1034294400,
1034553600, 1034640000, 1034726400, 1034812800, 1034899200, 1035158400,
1035244800, 1035331200, 1035417600, 1035504000, 1035763200, 1035849600,
1035936000, 1036022400, 1036108800, 1036368000, 1036454400, 1036540800,
1036627200, 1036713600, 1036972800, 1037059200, 1037145600, 1037232000,
1037318400, 1037577600, 1037664000, 1037750400, 1037836800, 1037923200,
1038182400, 1038268800, 1038355200, 1038441600, 1038528000, 1038787200,
1038873600, 1038960000, 1039046400, 1039132800, 1039392000, 1039478400,
1039564800, 1039651200, 1039737600, 1039996800, 1040083200, 1040169600,
1040256000, 1040342400, 1040601600, 1040688000, 1040774400, 1040860800,
1040947200, 1041206400, 1041292800), tzone = "UTC", tclass = "Date"), .Dim = c(522L,
1L), .Dimnames = list(NULL, "Series1"))
Series2:
structure(c(100, 100.32, 100.57, 100.82, 100.98, 101.01, 101.16,
101.3, 101.75, 102.07, 102.12, 102.3, 102.44, 102.59, 102.62,
102.74, 102.84, 103.09, 103.25, 103.31, 103.35, 103.48, 103.6,
103.72, 103.84, 103.96, 104.1, 104.35, 104.52, 104.69, 104.82,
104.96, 104.9, 105.03, 105.08, 105.27, 105.46, 105.55, 105.78,
105.94, 106.11, 106.36, 106.52, 106.6, 106.7, 106.92, 107.1,
107.27, 107.39, 107.41, 107.54, 107.72, 107.96, 108.13, 108.3,
108.43, 108.56, 108.68, 108.77), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", class = c("xts",
"zoo"), index = structure(c(1010102400, 1010707200, 1011312000,
1011916800, 1012435200, 1012521600, 1013126400, 1013731200, 1014336000,
1014854400, 1014940800, 1015545600, 1016150400, 1016755200, 1017360000,
1017964800, 1018569600, 1019174400, 1019779200, 1020124800, 1020384000,
1020988800, 1021593600, 1022198400, 1022803200, 1023408000, 1024012800,
1024617600, 1025222400, 1025827200, 1026432000, 1027036800, 1027641600,
1028073600, 1028246400, 1028851200, 1029456000, 1030060800, 1030665600,
1031270400, 1031875200, 1032480000, 1033084800, 1033344000, 1033689600,
1034294400, 1034899200, 1035504000, 1036022400, 1036108800, 1036713600,
1037318400, 1037923200, 1038528000, 1039132800, 1039737600, 1040342400,
1040947200, 1041292800), tzone = "UTC", tclass = "Date"), .Dim = c(59L,
1L), .Dimnames = list(NULL, "Series2"))
na.locf() will do the job:
Test<-merge.xts(Series1, Series2, join="left", fill=na.locf())
This function "Last Observation Carried Forward" fills the NA with the last knowne value.
Hope this helps people landing here, years after you asked the question.

Selecting dates and time interval from observations in R

Having an object of class zoo we can select observations for a range of dates of interest using the function:
window(z, start = as.Date("2006-01-05"), end = as.Date("2006-01-08"))
In running this function the following warning message occurs:
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 "<="
I have checked that the object is of class zoo and that Dates are included in the time series.
How is that possible?
Below the str(z) as requested:
‘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" ...
Below the dput(head(z)) as requested:
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")
You shouldn't be comparing Date with POSIXct
Try this:
window(z, start = as.POSIXct("2006-01-05"), end = as.POSIXct("2006-01-08"))
Alternatively, as #JoshuaUlrich points out in a comment, your data is daily frequency, so you'd be better off using a Date index class for your data to avoid timezone weirdness.
index(z) <- as.Date(index(z))
window(z, start = as.Date("2006-01-05"), end = as.Date("2006-01-08"))
It looks like window doesn't convert between Date and POSIXct automatically. You have to specify the times in the same class as in the data:
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

Resources