Question

Iam trying to merge zoo objects with different index classes. Because the index classes are different I have tried to aggregate the the first zoo object as index class date.

> dput(ldr_fund)
structure(c(0, 0.00711526523549466, -0.00967898190752514, 0.00865649238073551, 
0.00140576371796719, -0.00277816737879455, 0.00299548328067445, 
-0.000337635111039347, 0.0032196072133992, -0.000270802406319604, 
-0.00069783616594421, 0.000224839739512817, 0.00571506572744696, 
-0.00746006714414271, -0.00365790751321349, 0.0107331741176058, 
-0.0147292166162796, 0.00876201646252461, 0.0119990814059783, 
-0.000439798929239288, 0.00216263784779791, 0.00331579614744903, 
0.00864493546867529, 0.00389976681281468, 0.00229305793467383, 
0.00367984209717065, 0.00320868237916372, 5.98484992213599e-05, 
-0.000289401793452271, 0.00523584702691515, -0.0018435112082944, 
-0.00343597242446236, 0.000591752944682611, 0.00399993442909263, 
-0.00643493976659926, 0.0068658180559904, -0.00387529228431127, 
0.00757751368715986, -0.00231678532792579, 0.00271447638126432, 
-0.00101444593213351, 0.00418953640464537, -0.00760980263702216, 
0.0046306972371184, -0.00293437923621731, 0.00484572903010161, 
0.00442672454119553, 0.00974585027789665, 0.00160686478219851, 
-0.000140334149801191, -0.0162229323139407, 0.0110307772371225, 
-0.00863443855846491, -0.00294479357881627, 0.00620598434056596, 
0.00355138801309529, 0.00886464493295591, -0.00346148036934224, 
0.00114459270716871, 0.000498442997617587, 0.00668682978367841, 
-0.000443061012932233), index = structure(c(1359586800, 1359673200, 
1359932400, 1360018800, 1360105200, 1360191600, 1360278000, 1360537200, 
1360623600, 1360710000, 1360796400, 1360882800, 1361228400, 1361314800, 
1361401200, 1361487600, 1361746800, 1361833200, 1361919600, 1362006000, 
1362092400, 1362351600, 1362438000, 1362524400, 1362610800, 1362697200, 
1362956400, 1363042800, 1363129200, 1363215600, 1363302000, 1363561200, 
1363647600, 1363734000, 1363820400, 1363906800, 1364166000, 1364252400, 
1364338800, 1364425200, 1364767200, 1364853600, 1364940000, 1365026400, 
1365112800, 1365372000, 1365458400, 1365544800, 1365631200, 1365717600, 
1365976800, 1366063200, 1366149600, 1366236000, 1366322400, 1366581600, 
1366668000, 1366754400, 1366840800, 1366927200, 1367186400, 1367272800
), class = c("POSIXct", "POSIXt"), tzone = ""), class = "zoo", .Names = c("2013-01-31", 
"2013-02-01", "2013-02-04", "2013-02-05", "2013-02-06", "2013-02-07", 
"2013-02-08", "2013-02-11", "2013-02-12", "2013-02-13", "2013-02-14", 
"2013-02-15", "2013-02-19", "2013-02-20", "2013-02-21", "2013-02-22", 
"2013-02-25", "2013-02-26", "2013-02-27", "2013-02-28", "2013-03-01", 
"2013-03-04", "2013-03-05", "2013-03-06", "2013-03-07", "2013-03-08", 
"2013-03-11", "2013-03-12", "2013-03-13", "2013-03-14", "2013-03-15", 
"2013-03-18", "2013-03-19", "2013-03-20", "2013-03-21", "2013-03-22", 
"2013-03-25", "2013-03-26", "2013-03-27", "2013-03-28", "2013-04-01", 
"2013-04-02", "2013-04-03", "2013-04-04", "2013-04-05", "2013-04-08", 
"2013-04-09", "2013-04-10", "2013-04-11", "2013-04-12", "2013-04-15", 
"2013-04-16", "2013-04-17", "2013-04-18", "2013-04-19", "2013-04-22", 
"2013-04-23", "2013-04-24", "2013-04-25", "2013-04-26", "2013-04-29", 
"2013-04-30"))
> dput(ldr_dj_bm_2007)
structure(c(0, 0.0107075310151732, -0.00930165219240386, 0.00712294527047419, 
0.000516344606410257, -0.00304111464474488, 0.00350216665922076, 
-0.00155412968031143, 0.00339122166635697, -0.00255628297725785, 
-0.00068106297099213, 0.000598816336173869, 0.00384832345665487, 
-0.00773377154828836, -0.00337455225556305, 0.00860442040322518, 
-0.0155772107827197, 0.00837736032144853, 0.0125282688135684, 
-0.00148454374353513, 0.00249927734944144, 0.00270470809321743, 
0.00887553000438146, 0.00297513250094283, 0.00232308590582342, 
0.00470506212171529, 0.00348214006095837, 0.000191713081544975, 
0.000361178941796325, 0.00578457743516481, -0.00172304345934293, 
-0.00428431476528957, 0.000260136683921885, 0.00386018627232154, 
-0.00623783267864297, 0.00625850539644546, -0.0044392675334084, 
0.00771531092615163, -0.00230284216255505, 0.00359942253672152, 
-0.000390375901270446, 0.00609958634538543, -0.00764474652535263, 
0.00382488601013797, -0.00280137962175964, 0.00330583571825471, 
0.00409602941511089, 0.00873810083621329, 0.00424035383294985, 
-5.38173305209e-06, -0.0180467600763894, 0.0107359054985476, 
-0.00940863174878537, -0.00558725244663094, 0.00071309095618588, 
0.00135052166042549, 0.0104000612330069, -0.00293648002390334, 
0.00166796625127397, 0.0007989569787199, 0.00719239978027097, 
0.00141948972789052), index = structure(c(15736, 15737, 15740, 
15741, 15742, 15743, 15744, 15747, 15748, 15749, 15750, 15751, 
15755, 15756, 15757, 15758, 15761, 15762, 15763, 15764, 15765, 
15768, 15769, 15770, 15771, 15772, 15775, 15776, 15777, 15778, 
15779, 15782, 15783, 15784, 15785, 15786, 15789, 15790, 15791, 
15792, 15796, 15797, 15798, 15799, 15800, 15803, 15804, 15805, 
15806, 15807, 15810, 15811, 15812, 15813, 15814, 15817, 15818, 
15819, 15820, 15821, 15824, 15825), class = "Date"), class = "zoo", .Names = c("2013-01-31", 
"2013-02-01", "2013-02-04", "2013-02-05", "2013-02-06", "2013-02-07", 
"2013-02-08", "2013-02-11", "2013-02-12", "2013-02-13", "2013-02-14", 
"2013-02-15", "2013-02-19", "2013-02-20", "2013-02-21", "2013-02-22", 
"2013-02-25", "2013-02-26", "2013-02-27", "2013-02-28", "2013-03-01", 
"2013-03-04", "2013-03-05", "2013-03-06", "2013-03-07", "2013-03-08", 
"2013-03-11", "2013-03-12", "2013-03-13", "2013-03-14", "2013-03-15", 
"2013-03-18", "2013-03-19", "2013-03-20", "2013-03-21", "2013-03-22", 
"2013-03-25", "2013-03-26", "2013-03-27", "2013-03-28", "2013-04-01", 
"2013-04-02", "2013-04-03", "2013-04-04", "2013-04-05", "2013-04-08", 
"2013-04-09", "2013-04-10", "2013-04-11", "2013-04-12", "2013-04-15", 
"2013-04-16", "2013-04-17", "2013-04-18", "2013-04-19", "2013-04-22", 
"2013-04-23", "2013-04-24", "2013-04-25", "2013-04-26", "2013-04-29", 
"2013-04-30")) 

The first zoo object before the aggregation looks like this:

> str(ldr_fund)
‘zoo’ series from 2013-01-31 to 2013-04-30
  Data: Named num [1:62] 0 0.01109 -0.00989 0.0092 0.00133 ...
 - attr(*, "names")= chr [1:62] "2013-01-31" "2013-02-01" "2013-02-04" "2013-02-05" ...
  Index:  POSIXct[1:62], format: "2013-01-31" "2013-02-01" "2013-02-04" "2013-02-05" "2013-02-06" "2013-02-07" ...

Now comes the aggregation:

ldr_fund <- aggregate(ldr_fund, as.Date)

And it looks like this:

> str(ldr_fund)
‘zoo’ series from 2013-01-30 to 2013-04-29
  Data: num [1:62] 0 0.01109 -0.00989 0.0092 0.00133 ...
  Index:  Date[1:62], format: "2013-01-30" "2013-01-31" "2013-02-03" "2013-02-04" "2013-02-05" "2013-02-06" ...

The problem is that the time series starts now from 2013-01-30 instead of 2013-01-31. After the merge I got a lot of NA´s because the dates are different... What Iam doing wrong? It could be that I have one or to missung values in my real data from 2007 which i would also like to inspect by merging.

Was it helpful?

Solution

What date we get depends on what time zone is assumed during the conversion from POSIXct to Date since the date in one time zone could be different than the date in some other time zone.

My time zone is 4 hours before GMT so:

> tt <- as.POSIXct("2013-05-20 22:00:00")
> as.Date(tt) # it gives date in GMT by default & 10pm here is 2am the next day in GMT
[1] "2013-05-21"
> as.Date(tt, tz = "") # do conversion relative to local time zone
[1] "2013-05-20"

As discussed in R News 4/1 you can avoid these problems by using a class which has no time zones (i.e. chron) if you don't really need time zones but it may be too late if you already have the data in POSIXct form in which case you can use:

aggregate(ldr_fund, function(tt) as.Date(tt, tz = "...whatever...")) 

Often tz = "" (which means local time zone) will give the intended result if the default of "GMT" (or "UTC") does not; however, whether that will or not will give the intended result will depend on what you have, what you want and what time zone you are in.

Alternately we can add or subtract enough seconds to each POSIXct time and then use the same aggregate statement you used in the question (where X is some number of positive or negative seconds):

time(ldr_fund) <- time(ldr_fund) + X

Note that this has nothing to do with zoo but is how POSIXct works. zoo does not require that you use POSIXct and does not do time processing itself but relies on whatever date/time class you decided to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top