将数据帧列表导出到 r 中的 csv

Export a list of dataframes to a csv in r

我想以 CSV 格式导出下面提到的数据帧列表

数据帧列表的结构multidf

structure(list(`0` = structure(list(X01.04.2020 = 0:1, Asset = c("PORTFOLIO", "CASH"), Position = c("--", "--"), Price = c("--", "--"), Mark.to.Market = c(1000000L, 1000000L)), class = "data.frame", row.names = c(NA, -2L)), `1` = structure(list(X02.04.2020 = c(0L, 1L, 2L, 3L, NA), Asset = c("PORTFOLIO", "CASH", "FUTURES", "VXc1", "Total"), Position = c("--", "--", "500", "-5931", "Buys:"), Price = c("--", "--", "2516", "47", "1"), Mark.to.Market = c("999231", "509866", "1258250", "-279795", "Sells:"), Position.prior = c(NA, NA, 0L, 0L, 1L), Transaction = c("", "", "500", "-5931", "TC spent:"), TC.spent = c(NA, NA, 629L, 140L, 769L), DOWN = c("", "", "", "", ""), UP = c("", "", "", "", "")), row.names = c(NA, -5L), class = "data.frame"), `2` = structure(list(X03.04.2020 = c(0L, 1L, 2L, 3L, NA), Asset = c("PORTFOLIO", "CASH", "FUTURES", "VXc1", "Total"), Position = c("--", "--", "505", "-6206", "Buys:"), Price = c("--", "--", "2483", "45", "1"), Mark.to.Market = c("995708", "508078", "1253789", "-278805", "Sells:"), Position.prior = c(NA, NA, 500L, -5931L, 1L), Transaction = c("", "", "5", "-275", "TC spent:"), TC.spent = c(NA, NA, 6L, 6L, 12L), DOWN = c("", "", "", "", ""), UP = c("", "", "", "", "")), row.names = c(NA, -5L), class = "data.frame"), `3` = structure(list(X06.04.2020 = c(0L, 1L, 2L, 3L, NA), Asset = c("PORTFOLIO", "CASH", "FUTURES", "VXc1", "Total"), Position = c("--", "--", "522", "-7352", "Buys:"), Price = c("--", "--", "2644", "42", "1"), Mark.to.Market = c("1096915", "559989", "1380429", "-307130", "Sells:"), Position.prior = c(NA, NA, 505L, -6206L, 1L), Transaction = c("", "", "17", "-1146", "TC spent:"), TC.spent = c(NA, NA, 22L, 24L, 46L), DOWN = c("", "", "", "", ""), UP = c("", "", "", "", "")), row.names = c(NA, -5L), class = "data.frame"), `4` = structure(list(X07.04.2020 = c(0L, 1L, 2L, 3L, NA), Asset = c("PORTFOLIO", "CASH", "FUTURES", "VXc1", "Total"), Position = c("--", "--", "514", "-6863", "Buys:"), Price = c("--", "--", "2642", "44", "1"), Mark.to.Market = c("1079069", "550870", "1357988", "-302144", "Sells:"), Position.prior = c(NA, NA, 522L, -7352L, 1L), Transaction = c("", "", "-8", "489", "TC spent:"), TC.spent = c(NA, NA, 11L, 11L, 21L), DOWN = c("", "", "", "", ""), UP = c("", "", "", "", "")), row.names = c(NA, -5L), class = "data.frame"), `5` = structure(list(X08.04.2020 = c(0L, 1L, 2L, 3L, NA), Asset = c("PORTFOLIO", "CASH", "FUTURES", "VXc1", "Total"), Position = c("--", "--", "524", "-7521", "Buys:"), Price = c("--", "--", "2735", "42", "1"), Mark.to.Market = c("1138189", "580800", "1433140", "-318702", "Sells:"), Position.prior = c(NA, NA, 514L, -6863L, 1L), Transaction = c("", "", "10", "-658", "TC spent:"), TC.spent = c(NA, NA, 14L, 14L, 28L), DOWN = c("", "", "", "", ""), UP = c("", "", "", "", "")), row.names = c(NA, -5L), class = "data.frame")), .Dim = 6L, .Dimnames = list(`cumsum(!grepl("\S", txt))` = c("0", "1", "2", "3", "4", "5")))

我使用了以下代码,但显示错误

write.list(multidf, "filename.csv")

错误信息

Error in write.list(multidf, "Trade-Port-Pred-02022021-to-13022021.csv") : Need an 'list' object.

预期的结果应该是 请帮我解决这个问题。 提前致谢。

我们可以使用for循环并使用write.table在每个数据后插入一个空行。

for (data in multidf) {
  write.table(rbind(data, ''), file="temp.csv",
              append=TRUE,sep = ',', col.names = TRUE, row.names = FALSE)
}