如何使用xts对象列表,如R中的3d数组?

问题描述 投票:0回答:1

我想以一种可以用作3d数组的方式访问xts对象的列表。

这里是样本数据。因为这只是一个示例,所以我在所有对象中保留了相同的数据。

data(sample_matrix)
sample.xts_1 <- as.xts(sample_matrix, descr='Object 1')
sample.xts_2 <- as.xts(sample_matrix, descr='Object 2')
sample.xts_3 <- as.xts(sample_matrix, descr='Object 3')

data_list = list(a = sample.xts_1, b = sample.xts_2, c = sample.xts_3)

这个数据看起来像这样-

$a
           Open High  Low Close
2007-01-02 50.0 50.1 50.0  50.1
2007-01-03 50.2 50.4 50.2  50.4
$b
           Open High  Low Close
2007-01-02 50.0 50.1 50.0  50.1
2007-01-03 50.2 50.4 50.2  50.4
$c
           Open High  Low Close
2007-01-02 50.0 50.1 50.0  50.1
2007-01-03 50.2 50.4 50.2  50.4

是否有一种简单的方法可以以下方式访问这些列表元素?

$open
            a    b    c 
2007-01-02 50.0 50.0 50.0 
2007-01-03 50.2 50.2 50.2 
2007-01-04 50.4 50.4 50.4 
2007-01-05 50.4 50.4 50.4 
2007-01-06 50.2 50.2 50.2 
2007-01-07 50.1 50.1 50.1 
2007-01-08 50.0 50.0 50.0 

$high
            a    b    c 
2007-01-02 50.1 50.1 50.1 
2007-01-03 50.4 50.4 50.4 
2007-01-04 50.4 50.4 50.4 
2007-01-05 50.4 50.4 50.4 
2007-01-06 50.2 50.2 50.2 
2007-01-07 50.2 50.2 50.2 
2007-01-08 50.1 50.1 50.1 

$low
            a    b    c 
2007-01-02 50.0 50.0 50.0 
2007-01-03 50.2 50.2 50.2 
2007-01-04 50.3 50.3 50.3 
2007-01-05 50.2 50.2 50.2 
2007-01-06 50.1 50.1 50.1 
2007-01-07 50.0 50.0 50.0 
2007-01-08 50.0 50.0 50.0 


$close
            a    b    c 
2007-01-02 50.1 50.1 50.1 
2007-01-03 50.4 50.4 50.4 
2007-01-04 50.3 50.3 50.3 
2007-01-05 50.3 50.3 50.3 
2007-01-06 50.2 50.2 50.2 
2007-01-07 50.0 50.0 50.0 
2007-01-08 50.0 50.0 50.0 
r arrays list 3d xts
1个回答
0
投票

一个选项是按列splittranspose输出和cbind一起

library(purrr)
out <- map(data_list, ~ asplit(.x, 2)) %>%
      transpose %>%
      map(~do.call(cbind, .x))

map(out, head, 3)
#$Open
#                  a        b        c
#2007-01-02 50.03978 50.03978 50.03978
#2007-01-03 50.23050 50.23050 50.23050
#2007-01-04 50.42096 50.42096 50.42096

#$High
#                  a        b        c
#2007-01-02 50.11778 50.11778 50.11778
#2007-01-03 50.42188 50.42188 50.42188
#2007-01-04 50.42096 50.42096 50.42096

#$Low
#                  a        b        c
#2007-01-02 49.95041 49.95041 49.95041
#2007-01-03 50.23050 50.23050 50.23050
#2007-01-04 50.26414 50.26414 50.26414

#$Close
#                  a        b        c
#2007-01-02 50.11778 50.11778 50.11778
#2007-01-03 50.39767 50.39767 50.39767
#2007-01-04 50.33236 50.33236 50.33236
© www.soinside.com 2019 - 2024. All rights reserved.