将一个矩阵列表很好地导出到Excel中的同一个工作表

时间:2012-10-22 07:38:21

标签: r xlconnect

我有一个包含4个矩阵的列表。前两个:

$`1857-1903`
                      Bank.o.kassa Obligationer       Lån    Aktier Placeringar.andra.ftg Fodringar Reala.tillgångar Övriga.tillgångar
Bank.o.kassa             1.0000000    0.8014382 0.8079718 0.4421687                    NA 0.8395201        0.9144023         0.7995480
Obligationer             0.8014382    1.0000000 0.8422192 0.4590398                    NA 0.7871074        0.8815054         0.7003110
Lån                      0.8079718    0.8422192 1.0000000 0.5476716                    NA 0.8932723        0.9295665         0.7846206
Aktier                   0.4421687    0.4590398 0.5476716 1.0000000                    NA 0.7203681        0.4221815         0.6281095
Placeringar.andra.ftg           NA           NA        NA        NA                     1        NA               NA                NA
Fodringar                0.8395201    0.7871074 0.8932723 0.7203681                    NA 1.0000000        0.8869905         0.9395209
Reala.tillgångar         0.9144023    0.8815054 0.9295665 0.4221815                    NA 0.8869905        1.0000000         0.8158413
Övriga.tillgångar        0.7995480    0.7003110 0.7846206 0.6281095                    NA 0.9395209        0.8158413         1.0000000
Kapitalinkomster         0.9163688    0.8960790 0.9318508 0.4985449                    NA 0.9277740        0.9821501         0.8755644
                      Kapitalinkomster
Bank.o.kassa                 0.9163688
Obligationer                 0.8960790
Lån                          0.9318508
Aktier                       0.4985449
Placeringar.andra.ftg               NA
Fodringar                    0.9277740
Reala.tillgångar             0.9821501
Övriga.tillgångar            0.8755644
Kapitalinkomster             1.0000000

$`1904-1948`
                      Bank.o.kassa Obligationer         Lån       Aktier Placeringar.andra.ftg   Fodringar Reala.tillgångar Övriga.tillgångar
Bank.o.kassa             1.0000000   -0.5636371  0.12902979 -0.658883574          -0.202688601  0.76474140       0.81544129         0.4642105
Obligationer            -0.5636371    1.0000000  0.23820065  0.369712399           0.649156560 -0.69097442      -0.67090178        -0.3108845
Lån                      0.1290298    0.2382006  1.00000000 -0.301682319           0.663984986 -0.07873989       0.00901089         0.2894697
Aktier                  -0.6588836    0.3697124 -0.30168232  1.000000000          -0.005049947 -0.69634496      -0.77966741        -0.7496402
Placeringar.andra.ftg   -0.2026886    0.6491566  0.66398499 -0.005049947           1.000000000 -0.44833419      -0.31431360         0.1089602
Fodringar                0.7647414   -0.6909744 -0.07873989 -0.696344963          -0.448334185  1.00000000       0.84590718         0.5765548
Reala.tillgångar         0.8154413   -0.6709018  0.00901089 -0.779667406          -0.314313596  0.84590718       1.00000000         0.5367746
Övriga.tillgångar        0.4642105   -0.3108845  0.28946968 -0.749640198           0.108960190  0.57655477       0.53677459         1.0000000
Kapitalinkomster        -0.1114329    0.3693150  0.38160001 -0.125296598           0.720230427 -0.17280772      -0.13946215         0.3720201
                      Kapitalinkomster
Bank.o.kassa                -0.1114329
Obligationer                 0.3693150
Lån                          0.3816000
Aktier                      -0.1252966
Placeringar.andra.ftg        0.7202304
Fodringar                   -0.1728077
Reala.tillgångar            -0.1394622
Övriga.tillgångar            0.3720201
Kapitalinkomster             1.0000000

我想将它们导出到Excel中的相同工作表。问题是,如果我,例如,做 write.csv2(my.list,file="my.list.csv2") 矩阵没有分开,它们之间没有间距。另一方面,我知道如何使用XLConnect将我的列表导出到具有多个工作表的工作簿(在本例中为4)。但是我希望我的矩阵在相同的工作表中,以一些间距分隔,并带有列表名(即$ 1857-1903)。如果rownames存在也会很好......有可能吗?找不到任何答案。 最诚挚的问候!

修改

我接受了@January的回答。谢谢!但是,如果我使用write.csv2而不是write.table(为了获得所需的输出),我会收到一条警告消息:

In write.csv2(export, file = "funkcorr.csv", quote = F, sep = ",") : attempt to set 'sep' ignored。 然后,Excel中的数字与。代替 ,。所以,我需要手动转换。在Excel中。知道忽略sep的原因吗?

3 个答案:

答案 0 :(得分:3)

进一步研究这个问题,我仍然认为你可以使用sink()来解决问题,但我不确定是否有更好的选择。这是一个基本的例子:

示例数据

set.seed(1)
myList <- list(matrixA = matrix(sample(300, 60), nrow = 10,
                                dimnames = list(LETTERS[1:10], 
                                                paste0("V", 1:6))),
               matrixB = matrix(sample(300, 50), nrow = 10,
                                dimnames = list(LETTERS[1:10], 
                                                paste0("V", 1:5))))

使用sink()lapply()

sink("something.else.csv", type="output")
invisible(lapply(names(myList), 
                 function(x) { print(x)
                               dput(write.csv(myList[[x]])) } ))
sink()

以下是生成的CSV文件的外观:

[1] "matrixA"
"","V1","V2","V3","V4","V5","V6"
"A",80,296,262,131,214,120
"B",112,52,290,162,168,215
"C",171,198,182,133,202,109
"D",270,111,35,50,143,61
"E",60,221,74,286,136,291
"F",266,142,107,178,258,25
"G",278,204,4,210,6,78
"H",194,281,105,29,121,127
"I",184,108,237,190,185,161
"J",18,219,93,282,174,99
NULL
[1] "matrixB"
"","V1","V2","V3","V4","V5"
"A",274,297,122,65,171
"B",88,243,199,16,92
"C",137,100,112,173,70
"D",99,96,91,234,256
"E",193,298,209,208,163
"F",77,291,56,212,55
"G",141,246,195,121,33
"H",225,111,34,108,264
"I",25,220,67,213,233
"J",255,270,39,158,151
NULL

这在任何电子表格程序中都可以正常使用,您可以轻松搜索“NULL”的所有实例,并将其替换为任何内容,以便在每个矩阵之间留空行。当然,如果您需要,也可以使用write.csv2()代替write.csv()

答案 1 :(得分:2)

假设所有矩阵具有相同的列数,并且该列表称为matrices.l:

export <- NULL
nc <- ncol( matrices.l[[1]] )

for( n in names( matrices.l ) ) 
  export <- rbind( export, c( n, rep( "", nc - 1 ) ), matrices.l[[n]], "" )

write.table( export, file= ...,  quote= F, sep= ",", ... )

这不是很优雅,并且是在for循环中创建R对象的主要原因,但是对于四个矩阵来说也是如此。

答案 2 :(得分:2)

以下是基于XLConnect的解决方案:

# Preparation of dummy data
data = rep(list(WorldPhones), 5)
names(data) = LETTERS[1:5]

require(XLConnect)
wb = loadWorkbook("matrix.xlsx", create = TRUE)
# Create a new sheet
createSheet(wb, name = "mysheet")
# cumulative length (rows) of matrices
# +2 = 1 for list names, 1 for header row
cumlen = cumsum(c(1, head(sapply(data, nrow), n = -1) + 2))
# Write data rows (implicitly vectorized!)
writeWorksheet(wb, data = data, sheet = "mysheet", startRow = cumlen + 1, header = TRUE)
# Write list names
writeWorksheet(wb, data = as.list(names(data)), sheet = "mysheet", startRow = cumlen, header = FALSE)
saveWorkbook(wb)