在 R 中,将此类列表保存为永久文件的最佳方法是什么?

In R, what is the best way to save this type of list as a permanent file?

我的问题很简单:我在R中生成了这种类型的对象,它看起来像一个数据框,但实际上是一个数组。

> confidence
, , 1

          [,1]        [,2]
2.5%  16.98751 -0.03834785
97.5% 17.58275 -0.01476014

, , 2

          [,1]        [,2]
2.5%  18.69040 -0.06682464
97.5% 19.63179 -0.03040943

, , 3

          [,1]        [,2]
2.5%  20.41596 -0.09509432
97.5% 21.58752 -0.05110339

> class(confidence)
[1] "array"

我正在寻找一种将其导出为有用格式的方法,例如使用 excel 对其进行处理。如果需要,我愿意接受任何类型的对象转换。

感谢您的帮助!

一些建议:(1) 您可以展平数组,然后导出为一个数据框,或者 (2) 您可以使用 [=] 将每个数组元素导出到 excel sheet 13=]包。

数据:

vector1 <- rnorm(2, 18, 1)
vector2 <- runif(3)
array_data <- array(c(vector1,vector2),dim = c(2,2,3))

扁平化方法:

apply(array_data, MARGIN = 2, as.matrix)

#             [,1]       [,2]
# [1,] 19.6464748  0.3473246
# [2,] 18.1929962  0.0200243
# [3,]  0.5028130 18.1929962
# [4,] 19.6464748  0.3473246
# [5,]  0.0200243 19.6464748
# [6,]  0.5028130 18.1929962

# Can export normally, i.e. 
# write.csv(apply(array_data, MARGIN = 2, as.matrix), file = "array_data.csv")

导出到多个 sheet:

exp <- list('Array1' = array_data[,,1], 'Array2' = array_data[,,2], 'Array3' = array_data[,,3]) 
openxlsx::write.xlsx(exp, file = 'array_data.xlsx') 

输出:

1) adply 这会将 3d 数组转换为数据框,然后您可以使用 write.csv 并将其导入 Excel 或传输它使用其他 R 包。

# test input
a <- array(1:24, 2:4, dimnames = 
  list(dim1 = c("a", "b"), dim2 = c("A", "B", "C"), dim3 = 1:4))

plyr::adply(a, 1:2)

给予:

  dim1 dim2 1  2  3  4
1    a    A 1  7 13 19
2    b    A 2  8 14 20
3    a    B 3  9 15 21
4    b    B 4 10 16 22
5    a    C 5 11 17 23
6    b    C 6 12 18 24

2) ftable 另一种方法是使用ftable和ftable2df。后者在此 SO post: Reshaping an array to data.frame 或 github hockeyR 包中的 ftable2df 中定义。

# remotes::install_github("pbulsink/hockeyR")

h <- hockeyR::ftable2df(ftable(a)); h

给予:

  dim1 dim2 1  2  3  4
1    a    A 1  7 13 19
2    a    B 3  9 15 21
3    a    C 5 11 17 23
4    b    A 2  8 14 20
5    b    B 4 10 16 22
6    b    C 6 12 18 24

3) write.csv 注意 write.csv 可以将数组写入 csv 文件。

write.csv(a)

给予:

"","A.1","B.1","C.1","A.2","B.2","C.2","A.3","B.3","C.3","A.4","B.4","C.4"
"a",1,3,5,7,9,11,13,15,17,19,21,23
"b",2,4,6,8,10,12,14,16,18,20,22,24
a <- array(1:24, 2:4, dimnames = list(dim1 = c("a", "b"), dim2 = c("A", "B", "C"), dim3 = 1:4))

# make it a data.table in long format
b <- data.table::as.data.table(a)

# just save as any file
# read it back

#convert the data.table back to the 3 dimensional array
c <- xtabs(data = b, formula = value ~ .)

数据

a

# , , dim3 = 1
# 
#     dim2
# dim1 A B C
#    a 1 3 5
#    b 2 4 6
# 
# , , dim3 = 2
# 
#     dim2
# dim1 A  B  C
#    a 7  9 11
#    b 8 10 12
# 
# , , dim3 = 3
# 
#     dim2
# dim1  A  B  C
#    a 13 15 17
#    b 14 16 18
# 
# , , dim3 = 4
# 
#     dim2
# dim1  A  B  C
#    a 19 21 23
#    b 20 22 24

b 的输出写入 csv 或 xls

b

#     dim1 dim2 dim3 value
#  1:    a    A    1     1
#  2:    a    A    2     7
#  3:    a    A    3    13
#  4:    a    A    4    19
#  5:    a    B    1     3
#  6:    a    B    2     9
#  7:    a    B    3    15
#  8:    a    B    4    21
#  9:    a    C    1     5
# 10:    a    C    2    11
# 11:    a    C    3    17
# 12:    a    C    4    23
# 13:    b    A    1     2
# 14:    b    A    2     8
# 15:    b    A    3    14
# 16:    b    A    4    20
# 17:    b    B    1     4
# 18:    b    B    2    10
# 19:    b    B    3    16
# 20:    b    B    4    22
# 21:    b    C    1     6
# 22:    b    C    2    12
# 23:    b    C    3    18
# 24:    b    C    4    24