我可以将xts对象写到excel中,包括行名

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

我正在尝试将xts对象列表写入同一xlsx Excel文件的多个工作表中。但是,我无法将xts对象的日期戳(即索引/行名)打印到电子表格中。

样本的Dput输出:

tempX <- structure(c(1469.25, 1455.22, 1399.42, 1402.11, 1403.45, 1441.47, 
8413.75, 8413.75, 8202.6, 8119.42, 8114.24, 8429.42, 4904.464003067, 
4849.2154769917, 4657.8280343277, 4541.7504569184, 4500.6868882609, 
4648.2684802394, 6958.14, 6750.77, 6586.95, 6502.07, 6474.92, 
6780.97, 5958.32, 5917.37, 5672.02, 5479.7, 5450.11, 5539.61, 
11641.4, 11641.4, 11641.4, 11641.4, 11641.4, 11102.4, 42605.09, 
41226.4, 40313.79, 39452.41, 38835.05, 40194.22, 6930.2001953125, 
6930.2001953125, 6665.85986328125, 6535.89990234375, 6447.240234375, 
6504.75, 7570.1, 7570.1, 7268.1, 7181.3, 7280.6, 7448, 1198.97, 
1211.79, 1182.03, 1131.3, NA, 1152.54, 189.7622, 192.7313, 185.698, 
180.5996, 182.556, 186.3997, 1722.2, 1722.2, 1717.47, 1651.84, 
1609.59, 1599.01, 3117.7, 3117.7, 3093.7, 3018.7, 2999.3, 3023.4, 
795.41, 795.41, 795.41, 778.94, 769.13, 775.02, 2526.67, 2608.48, 
2549.01, 2425.9, 2409, 2449.96, NA, NA, NA, NA, NA, NA, 16962.1, 
17369.63, 17072.82, 15846.72, 15153.23, 15405.63, 130.02, 130.02, 
133.66, 123.86, 120.8, 119.1, 8448.84, 8448.84, 8756.55, 8849.87, 
8922.03, 8845.47, 5005.82, 5375.11, 5491.01, 5357, 5421.53, 5414.48, 
676.92, 676.92, 700.218, 678.087, 688.52, 688.52, NA, NA, NA, 
NA, NA, NA, 481.92, 481.92, 498.46, 465.85, 443.46, 453.31, 2142.97, 
2141.77, 2153.18, 2074.75, 2079.11, 2094.29, 17091.6, 16930.42, 
15851, 16245.14, 16106.89, 16309.15, 1183.9999, 1196.9055, 1180.448, 
1182.6976, 1179.0272, 1186.3679, NA, NA, NA, NA, NA, NA, 1835.57, 
1828.51, 1811.8, 1810.15, 1798.3, 1805.4, 7129.88, 7077.71, 6675.27, 
6764.31, 6751.65, 7047.09, 177.711, 177.711, 177.711, 179.013, 
185.52, 185.52, 489.700012207031, 489.700012207031, 489.700012207031, 
484.5, 494.899993896484, 499.399993896484, 8819.45, 8819.45, 
8715.49, 8464.34, 8483.29, 8694.04, 1755.8, 1755.8, 1755.8, 1755.8, 
1755.8, 1867, 19367.95, 19367.95, 22406, 21475.12, 20136.46, 
20136.46, 7806.19228893826, 7806.19228893826, 7782.61164901041, 
7711.56507769325, 7734.47205735077, 7930.83497849444, 1121.298, 
1116.631, 1080.228, 1065.784, 1054.835, 1082.203, 21162.525, 
21375.162, 21529.527, 21143.61, 20985.277, 21162.627, 401.225, 
399.844, 387.625, 382.348, 378.477, 387.945, 1445.856, 1436.684, 
1379.114, 1382.706, 1370.889, 1418.262, 1070.067, 1070.067, 1042.334, 
1027.823, 1019.371, 1072.507, 1514.08, 1507.71, 1452.257, 1416.774, 
1408.51, 1440.81, 917.253, 891.287, 869.053, 858.264, 851.866, 
892.33, 1876.329, 1866.567, 1785.73, 1725.802, 1716.452, 1747.217, 
989.557, 984.826, 956.267, 932.079, 932.079, 954.29, 1420.486, 
1374.419, 1340.197, 1314.391, 1291.028, 1332.06, 1974.226, 1974.226, 
1903.947, 1865.047, 1842.726, 1854.44, 957.838, 957.838, 921.168, 
909.802, 921.259, 942.94, 8971.501, 9118.711, 8916.29, 8488.422, 
8488.422, 8602.339, 1361.535, 1384.298, 1340.54, 1307.875, 1322.038, 
1344.019, 1013.661, 1013.661, 1012.393, 976.372, 951.781, 950.662, 
617.334, 617.334, 611.597, 597.711, 594.889, 600.794, 111.773, 
111.773, 111.773, 108.4, 106.114, 108.075, 1580.038, 1624.165, 
1597.329, 1513.219, 1486.964, 1527.178, 33.48, 33.789, 34.857, 
33.441, 33.202, 34.868, 9231.456, 9367.822, 9243.954, 8613.471, 
8237.538, 8394.553, 226.54, 226.54, 243.479, 229.995, 224.257, 
226.38, 385.218, 385.218, 401.6, 404.191, 404.855, 399.201, 209.491, 
225.03, 231.345, 226.018, 227.831, 223.709, 899.728, 899.728, 
940.442, 902.837, 912.687, 912.687, 296.27, 305.27, 304.911, 
300.859, 299.439, 299.439, 204.95, 204.95, 211.466, 195.573, 
185.568, 190.133, 519.377, 520.911, 520.12, 499.477, 498.691, 
503.729, 61567517000, 61000030000, 57887805000, 58634990000, 
58419216000, 59097990000, 7907.071, 7833.313, 7381.75, 7443.319, 
7421.563, 7723.602, 1660.25, 1676.283, 1660.401, 1662.441, 1653.711, 
1651.635, 181.983, 181.983, 184.54, 188.313, 191.17, 195.83, 
344.6, 341.147, 336.329, 336.871, 335.121, 338.36, 222.982, 222.982, 
223.76, 223.908, 230.928, 230.976, 102.004, 102.004, 102.004, 
100.815, 102.64, 104.173, 724.876, 724.876, 724.586, 695.458, 
698.59, 709.082, 1373.301, 1439.36, 1405.158, 1334.53, 1361.752, 
1432.959, 245019.67, 245019.67, 284004.22, 273683.5, 256780.73, 
250581.64, 247.719, 247.719, 245.186, 242.618, 241.532, 249.525, 
NA, NA, NA, NA, NA, NA, 1296.258, 1288.599, 1240.588, 1208.28, 
1204.228, 1239.878, 1409.887, 1395.454, 1340.623, 1303.842, 1293.493, 
1336.79, NA, NA, NA, NA, NA, NA, 300.72, 300.443, 293.709, 285.719, 
282.436, 287.164), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", index = structure(c(946598400, 
946857600, 946944000, 947030400, 947116800, 947203200), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 
78L), .Dimnames = list(NULL, c("SP50", "168110", "183657", "187653", 
"180454", "180824", "167202", "180555", "180825", "188923", "186658", 
"180460", "186745", "186447", "180085", "153354", "180458", "181563", 
"180530", "180353", "180355", "FBMKLCI0", "180532", "181027", 
"180264", "188922", "160355", "181023", "180731", "181698", "170442", 
"180275", "188471", "181825", "168635", "990100", "891800", "892400", 
"984000", "912400", "990500", "928000", "925000", "972400", "938000", 
"982600", "975600", "975200", "957800", "939200", "903600", "955400", 
"998100", "MS302400", "934400", "941000", "915800", "935600", 
"105767", "105768", "105769", "860800", "907600", "848400", "915200", 
"917000", "960400", "105653", "920000", "934800", "961600", "979200", 
"971000", "MS133333", "991700", "106569", "FC0000CN", "899901"
)))

我尝试过:

outputFileName <- "Equity.xlsx"
wb <- tryCatch(loadWorkbook(outputFileName), error=function(e){createWorkbook()})

outputName <- "Price"
ifelse(outputName %in% sheets(wb),paste(outputName, "sheet exists", sep = " "),addWorksheet(wb, sheetName = outputName, gridLines = FALSE))

其次:

output <- cbind(Data = rownames(tempX), tempX)
writeData(wb, sheet = outputName, output, rowNames = TRUE, keepNA = TRUE)

output <- tempX
writeData(wb, sheet = sheetName, output, rowNames = TRUE, keepNA = TRUE)

两者均未设法打印时间戳。 index(output)dimnames.xts[[1]]也会产生NULL输出。

有什么想法吗?

r xlsx xts openxlsx
1个回答
0
投票
library(zoo) tempX <- fortify.zoo(tempX)
© www.soinside.com 2019 - 2024. All rights reserved.