根据 R 中的日期创建列表
Create a list based on dates in R
假设我的数据具有以下结构
library(xts)
structure(c(-0.108511, -0.446626999999999, 0.240643000000002,
1.679788, -2.278705, 0.0174959999999977, -0.23011, -1.458079,
-0.770809, -0.160931000000001, -0.884409000000002, -0.127797999999999,
0.231928, -0.125263, -0.188977999999999, -0.343629999999997,
-0.333669, 0.168738999999999, 1.249041, -1.41732, 0.0101289999999992,
-0.434269, -1.328651, -0.810048999999999, -0.0380099999999999,
0.0380099999999999, 0, 0, -0.0821299999999994, 0.0821299999999994,
-0.0905709999999971, -0.626428000000001, 0.3538, 2.56579, -2.109532,
0.0253410000000009, -0.243471, -1.732849, -0.921948999999998,
-0.354142, -1.454579, 0.0786300000000004, 1.08839, -0.248750999999999,
-0.975019000000001, -0.022001000000003, -0.474670999999997, 0.16667,
1.165852, -4.200741, 0.0207899999999981, 0.0436310000000013,
-1.246241, -0.48298, -0.0585299999999993, -2.37666, -0.73001,
0.898570000000001, -0.295430000000001, -0.59243, -0.0217520000000029,
-0.449507999999998, 0.164778999999999, 1.750981, -1.99885, 0.0310400000000008,
-0.249700999999998, -1.548811, -0.839808999999999, -0.35397,
-0.703519, 0.0034099999999988, -0.743641, 0, 0.602879999999999
), class = c("xts", "zoo"), index = structure(c(1246490100, 1246490400,
1246490700, 1246491000, 1246491300, 1246491600, 1246491900, 1246492200,
1246492800, 1246493100, 1246493400, 1246493700, 1246494000, 1246494300,
1246494600), tzone = "UTC", tclass = c("POSIXct", "POSIXt")), .Dim = c(15L,
5L), .Dimnames = list(NULL, c("rrp_nsw", "rrp_qld", "rrp_sa",
"rrp_tas", "rrp_vic")))
上面的数据是一个 xts 对象,我想用这些信息创建一个列表,其中的元素是每日信息。我想使用所提供的数据获得以下输出。
Mylist[[`2009-07-01`]]
-0.108511 -0.343630 -0.090571 -0.022001 -0.021752
-0.446627 -0.333669 -0.626428 -0.474671 -0.449508
0.240643 0.168739 0.353800 0.166670 0.164779
1.679788 1.249041 2.565790 1.165852 1.750981
-2.278705 -1.417320 -2.109532 -4.200741 -1.998850
0.017496 0.010129 0.025341 0.020790 0.031040
-0.230110 -0.434269 -0.243471 0.043631 -0.249701
-1.458079 -1.328651 -1.732849 -1.246241 -1.548811
Mylist[[`2009-07-02`]]
-0.770809 -0.810049 -0.921949 -0.482980 -0.839809
-0.160931 -0.038010 -0.354142 -0.058530 -0.353970
-0.884409 0.038010 -1.454579 -2.376660 -0.703519
-0.127798 0.000000 0.078630 -0.730010 0.003410
0.231928 0.000000 1.088390 0.898570 -0.743641
-0.125263 -0.082130 -0.248751 -0.295430 0.000000
-0.188978 0.082130 -0.975019 -0.592430 0.602880
split.xts
将其拆分为 xts 对象列表。
几点:
- 也可以仅使用“days”作为 split 的第二个参数,但这会发出警告,并且会给出未命名列表而不是命名列表。 (
options(xts_check_TZ = FALSE)
可用于消除警告,尽管这也可能影响其他功能。)
- 日期时间的日期取决于时区,默认为 UTC。例如,as.Date(time(x), tz = "") 将给出 x 相对于当前时区的分割日期。
split(x, as.Date(time(x)))
给予:
$`2009-07-01`
rrp_nsw rrp_qld rrp_sa rrp_tas rrp_vic
2009-07-01 23:15:00 -0.108511 -0.343630 -0.090571 -0.022001 -0.021752
2009-07-01 23:20:00 -0.446627 -0.333669 -0.626428 -0.474671 -0.449508
2009-07-01 23:25:00 0.240643 0.168739 0.353800 0.166670 0.164779
2009-07-01 23:30:00 1.679788 1.249041 2.565790 1.165852 1.750981
2009-07-01 23:35:00 -2.278705 -1.417320 -2.109532 -4.200741 -1.998850
2009-07-01 23:40:00 0.017496 0.010129 0.025341 0.020790 0.031040
2009-07-01 23:45:00 -0.230110 -0.434269 -0.243471 0.043631 -0.249701
2009-07-01 23:50:00 -1.458079 -1.328651 -1.732849 -1.246241 -1.548811
$`2009-07-02`
rrp_nsw rrp_qld rrp_sa rrp_tas rrp_vic
2009-07-02 00:00:00 -0.770809 -0.810049 -0.921949 -0.48298 -0.839809
2009-07-02 00:05:00 -0.160931 -0.038010 -0.354142 -0.05853 -0.353970
2009-07-02 00:10:00 -0.884409 0.038010 -1.454579 -2.37666 -0.703519
2009-07-02 00:15:00 -0.127798 0.000000 0.078630 -0.73001 0.003410
2009-07-02 00:20:00 0.231928 0.000000 1.088390 0.89857 -0.743641
2009-07-02 00:25:00 -0.125263 -0.082130 -0.248751 -0.29543 0.000000
2009-07-02 00:30:00 -0.188978 0.082130 -0.975019 -0.59243 0.602880
假设我的数据具有以下结构
library(xts)
structure(c(-0.108511, -0.446626999999999, 0.240643000000002,
1.679788, -2.278705, 0.0174959999999977, -0.23011, -1.458079,
-0.770809, -0.160931000000001, -0.884409000000002, -0.127797999999999,
0.231928, -0.125263, -0.188977999999999, -0.343629999999997,
-0.333669, 0.168738999999999, 1.249041, -1.41732, 0.0101289999999992,
-0.434269, -1.328651, -0.810048999999999, -0.0380099999999999,
0.0380099999999999, 0, 0, -0.0821299999999994, 0.0821299999999994,
-0.0905709999999971, -0.626428000000001, 0.3538, 2.56579, -2.109532,
0.0253410000000009, -0.243471, -1.732849, -0.921948999999998,
-0.354142, -1.454579, 0.0786300000000004, 1.08839, -0.248750999999999,
-0.975019000000001, -0.022001000000003, -0.474670999999997, 0.16667,
1.165852, -4.200741, 0.0207899999999981, 0.0436310000000013,
-1.246241, -0.48298, -0.0585299999999993, -2.37666, -0.73001,
0.898570000000001, -0.295430000000001, -0.59243, -0.0217520000000029,
-0.449507999999998, 0.164778999999999, 1.750981, -1.99885, 0.0310400000000008,
-0.249700999999998, -1.548811, -0.839808999999999, -0.35397,
-0.703519, 0.0034099999999988, -0.743641, 0, 0.602879999999999
), class = c("xts", "zoo"), index = structure(c(1246490100, 1246490400,
1246490700, 1246491000, 1246491300, 1246491600, 1246491900, 1246492200,
1246492800, 1246493100, 1246493400, 1246493700, 1246494000, 1246494300,
1246494600), tzone = "UTC", tclass = c("POSIXct", "POSIXt")), .Dim = c(15L,
5L), .Dimnames = list(NULL, c("rrp_nsw", "rrp_qld", "rrp_sa",
"rrp_tas", "rrp_vic")))
上面的数据是一个 xts 对象,我想用这些信息创建一个列表,其中的元素是每日信息。我想使用所提供的数据获得以下输出。
Mylist[[`2009-07-01`]]
-0.108511 -0.343630 -0.090571 -0.022001 -0.021752
-0.446627 -0.333669 -0.626428 -0.474671 -0.449508
0.240643 0.168739 0.353800 0.166670 0.164779
1.679788 1.249041 2.565790 1.165852 1.750981
-2.278705 -1.417320 -2.109532 -4.200741 -1.998850
0.017496 0.010129 0.025341 0.020790 0.031040
-0.230110 -0.434269 -0.243471 0.043631 -0.249701
-1.458079 -1.328651 -1.732849 -1.246241 -1.548811
Mylist[[`2009-07-02`]]
-0.770809 -0.810049 -0.921949 -0.482980 -0.839809
-0.160931 -0.038010 -0.354142 -0.058530 -0.353970
-0.884409 0.038010 -1.454579 -2.376660 -0.703519
-0.127798 0.000000 0.078630 -0.730010 0.003410
0.231928 0.000000 1.088390 0.898570 -0.743641
-0.125263 -0.082130 -0.248751 -0.295430 0.000000
-0.188978 0.082130 -0.975019 -0.592430 0.602880
split.xts
将其拆分为 xts 对象列表。
几点:
- 也可以仅使用“days”作为 split 的第二个参数,但这会发出警告,并且会给出未命名列表而不是命名列表。 (
options(xts_check_TZ = FALSE)
可用于消除警告,尽管这也可能影响其他功能。) - 日期时间的日期取决于时区,默认为 UTC。例如,as.Date(time(x), tz = "") 将给出 x 相对于当前时区的分割日期。
split(x, as.Date(time(x)))
给予:
$`2009-07-01`
rrp_nsw rrp_qld rrp_sa rrp_tas rrp_vic
2009-07-01 23:15:00 -0.108511 -0.343630 -0.090571 -0.022001 -0.021752
2009-07-01 23:20:00 -0.446627 -0.333669 -0.626428 -0.474671 -0.449508
2009-07-01 23:25:00 0.240643 0.168739 0.353800 0.166670 0.164779
2009-07-01 23:30:00 1.679788 1.249041 2.565790 1.165852 1.750981
2009-07-01 23:35:00 -2.278705 -1.417320 -2.109532 -4.200741 -1.998850
2009-07-01 23:40:00 0.017496 0.010129 0.025341 0.020790 0.031040
2009-07-01 23:45:00 -0.230110 -0.434269 -0.243471 0.043631 -0.249701
2009-07-01 23:50:00 -1.458079 -1.328651 -1.732849 -1.246241 -1.548811
$`2009-07-02`
rrp_nsw rrp_qld rrp_sa rrp_tas rrp_vic
2009-07-02 00:00:00 -0.770809 -0.810049 -0.921949 -0.48298 -0.839809
2009-07-02 00:05:00 -0.160931 -0.038010 -0.354142 -0.05853 -0.353970
2009-07-02 00:10:00 -0.884409 0.038010 -1.454579 -2.37666 -0.703519
2009-07-02 00:15:00 -0.127798 0.000000 0.078630 -0.73001 0.003410
2009-07-02 00:20:00 0.231928 0.000000 1.088390 0.89857 -0.743641
2009-07-02 00:25:00 -0.125263 -0.082130 -0.248751 -0.29543 0.000000
2009-07-02 00:30:00 -0.188978 0.082130 -0.975019 -0.59243 0.602880