Search code examples
rexport-to-csv

Export a list of dataframes to a csv in r


I want to export the below-mentioned list of dataframes in CSV

Structure of the list of data frames 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")))

I have used the following code but it shows me an error

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

Error message

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

The expected result should be enter image description here Please help me figure this out. Thanks in advance.


Solution

  • We can use for loop and use write.table inserting an empty row after each data.

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