Search code examples
rlistdataframesave

save list elements into separated dfs in R


I have a list containing 180.000 elements each represents data about an investor and a specific traded asset. I want to save all the elements of the list into single dataframes called df into a specific folder "dev/test-data/investors-singleass/" , so that I can later on apply a specific function on all the dfs of the folder

The list of my data has a structure similar to this

list(`4Z627.004125` = structure(list(investor = c("4Z627", "4Z627", 
"4Z627"), asset = c("004125", "004125", "004125"), datetime = c("2015-05-12", 
"2015-05-28", "2016-08-19"), Avgprice = c(169.4, 168, 162), operation = c(2000, 
1000, -3000), portfolio = c(2000, 3000, 0), last_port = c(0, 
2000, 3000), marketprice = c(169.4, 166.5, 161.75), portprice = c(169.4, 
168.933333333333, 0), G = c(0, 0, 1), gainminus = c(2, 0, 0), 
    numasset = c(5, 8, 13)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"))

Basically each elements of the list is an "investor" ID and an "asset" code for which i then have multiple other columns to work with


Solution

  • I would do it like this based on link

    df1 <- list(`4Z627.004125` = structure(list(investor = c("4Z627", "4Z627", 
                                                      "4Z627"), asset = c("004125", "004125", "004125"), 
                                         datetime = c("2015-05-12",   "2015-05-28", "2016-08-19"), 
                                         Avgprice = c(169.4, 168, 162), 
                                         operation = c(2000, 1000, -3000), portfolio = c(2000, 3000, 0),
                                         last_port = c(0,2000, 3000), marketprice = c(169.4, 166.5, 161.75), 
                                         portprice = c(169.4, 
                                                                                                                                                                                                                                                                                                                            168.933333333333, 0), G = c(0, 0, 1), gainminus = c(2, 0, 0), 
                                         numasset = c(5, 8, 13)), row.names = c(NA, -3L),
                                         class = c("tbl_df", "tbl", "data.frame")),
                `4Z628.004128` = structure(list(investor = c("4Z627", "4Z627", 
                                                             "4Z627"), asset = c("004125", "004125", "004125"), 
                                                datetime = c("2015-05-12",   "2015-05-28", "2016-08-19"), 
                                                Avgprice = c(169.4, 168, 162), 
                                                operation = c(2000, 1000, -3000), portfolio = c(2000, 3000, 0),
                                                last_port = c(0,2000, 3000), marketprice = c(169.4, 166.5, 161.75), 
                                                portprice = c(169.4, 
                                                              168.933333333333, 0), G = c(0, 0, 1), gainminus = c(2, 0, 0), 
                                                numasset = c(5, 8, 13)), row.names = c(NA, -3L),
                                           class = c("tbl_df", "tbl", "data.frame")))
    library(purrr)
    iwalk(df1, ~saveRDS(.x, paste0("dev/test-data/investors-singleass/", .y, '.RData')))
    

    You can get the data back into R with

    library(dplyr)
    
    df <- list.files(path = "dev/test-data/investors-singleass/", pattern = ".RData") %>% 
      map_dfr(readRDS)