0

我正在尝试将动物园对象与不同的索引类合并。因为索引类不同,我尝试将第一个动物园对象聚合为索引类日期。

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

聚合之前的第一个 zoo 对象如下所示:

> 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" ...

现在是聚合:

ldr_fund <- aggregate(ldr_fund, as.Date)

它看起来像这样:

> 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" ...

问题是时间序列现在从 2013-01-30 而不是 2013-01-31 开始。合并后我得到了很多 NA,因为日期不同......我做错了什么?可能是我在 2007 年的真实数据中有一个或遗漏的值,我也想通过合并来检查这些值。

4

1 回答 1

2

我们得到的日期取决于从 POSIXct 转换为 Date 期间假定的时区,因为一个时区中的日期可能与其他时区中的日期不同。

我的时区是格林威治标准时间前 4 小时,所以:

> 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"

正如R News 4/1中所讨论的,如果您真的不需要时区,则可以通过使用没有时区(即 chron)的类来避免这些问题,但如果您已经拥有 POSIXct 形式的数据,则可能为时已晚在这种情况下,您可以使用:

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

tz = ""如果默认的“GMT”(或“UTC”)没有,通常(这意味着本地时区)会给出预期的结果;但是,这是否会产生预期的结果将取决于您拥有什么,您想要什么以及您所在的时区。

或者,我们可以为每个 POSIXct 时间添加或减去足够的秒数,然后使用您在问题中使用的相同聚合语句(其中 X 是某个正数或负数秒数):

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

请注意,这与 zoo 无关,而是 POSIXct 的工作原理。zoo 不要求您使用 POSIXct 并且不会自己进行时间处理,而是依赖于您决定使用的任何日期/时间类。

于 2013-05-20T15:57:38.640 回答