简体   繁体   中英

Write list of lists to separate CSV files

I have a list (414 elements) which contains other lists of different lengths (ranging from 0 to 9). Each of those sublists has different numbers of rows and columns.

Some of the sublists are of length 1 like the one below:

tables_list[[1]]
     [,1]                         [,2]                
[1,] "ID Number"                  "ABCD"              
[2,] "Code"                       "1239463"             
[3,] "Version"                    "1"                 
[4,] "Name"                       "ABC"
[5,] "Status"                     "Open"         
[6,] "Currency"                   "USD"               
[7,] "Average"                    "No"                
[8,] "FX Rate"                    "2.47"    

Other sublists are of length 2 or higher like the one below:

tables_list[[17]]
[[1]]
      [,1]  [,2]                                                  [,3]  [,4]              [,5]            [,6]              [,7]          [,8] [,9]            
 [1,] ""    ""                                                    "USD" "Balance"         "Movement in"   "Aggregate"       "Overall"     ""   "Overall"       
 [2,] ""    ""                                                    ""    "brought forward" "year"          "annual"          "aggregate"   ""   "funded account"
 [3,] ""    ""                                                    ""    "from previous"   ""              "information"    "adjustments"  ""   ""              
 [4,] ""    ""                                                    ""    "year end"        ""              ""                ""            ""   ""              
 [5,] ""    ""                                                    ""    "1"               "2"             "3"               "4"           ""   "5"             
 [6,] "12"  "Value 1"                                             ""    "0"               "3,275,020"     "3,275,020"       ""            "0"  "3,275,020"     
 [7,] "13"  "Value 2"                                             ""    "0"               "0"             "0"               ""            "0"  "0"             
 [8,] "14"  "Value 3"                                             ""    "0"               "8,267,862"     "8,267,862"       ""            "0"  "8,267,862"     
 [9,] "15"  "Value 4"                                             ""    "0"               "(590,073,321)" "(590,073,321)"   ""            "0"  "(590,073,321)" 
[10,] "16"  "Value 5"                                             ""    "0"               "0"             "0"               ""            "0"  "0"             
[11,] "17"  "Value 6"                                             ""    "0"               "0"             "0"               ""            "0"  "0"             
[12,] "18"  "Value 7"                                             ""    "0"               "0"             "0"               ""            "0"  "0"             
[13,] "19"  "Value 8"                                             ""    "0"               "0"             "0"               ""            "0"  "0"             
[14,] "20"  "Value 9"                                             ""    "0"               "(459,222,782)" "(459,222,782)"   ""            "0"  "(459,222,782)" 

[[2]]
     [,1]               [,2]   [,3]                                                                  [,4]           
[1,] "Theme"            "Year" "Comment"                                                             "Created"      
[2,] "Line 17 Column 2" "N/A"  "Amounts are calculated according to recent standards"                "XXXXXXXXXXXX"
[3,] ""                 ""     "paid by XXXXXXXXXXXXX"                                               ""      

I am trying to export each of those lists to an individual csv file but I cannot figure out a way to do so. Does anyone have any ideas on how to approach this? I tried using mapply but I keep getting the following error:

Error in file(file, ifelse(append, "a", "w")) : 
  invalid 'description' argument
In addition: Warning message:
In if (file == "") file <- stdout() else if (is.character(file)) { :

 Error in file(file, ifelse(append, "a", "w")) : 
  invalid 'description' argument 

First you flatten the list appropriately , then you can loop over it in a the regular manner.

flattenlist <- function(x){  
  morelists <- sapply(x, function(xprime) class(xprime)[1]=="list")
  out <- c(x[!morelists], unlist(x[morelists], recursive=FALSE))
  if (sum(morelists)) { 
    Recall(out)
  } else {
    return(out)
  }
}

l <- list(a=list(1:2, b=2:4), 
          b=c("A", "B", "C"), 
          z=1, 
          m=matrix(4:1, 2), 
          d=data.frame(x=1:4, y=c(1, 3, 2, 4))
)

l.f <- flattenlist(l)

n <- paste0("robj_", names(l.f), ".csv")

sapply(1:length(l.f), function(x) write.csv(l.f[[x]], file=n[x]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM