I have a list of lists of lists of lists (I know, a lot of lists, in total around 6000 DataFrames).
The first list specifies the startmonth (January through December), the second one specifies the year (2002 - 2018), the third one contains the different sectors (for example Discretionary or Consumer Staples, in total 10) and the last one the quantile (1 through 5).
To be more clear: sector_prtf[["StartMonth"]][["Year"]][["Sector"]][["Quantile"]]
Here is an example of how the DataFrames look:
sector_prtf[[1]][[1]][[1]][[1]]
Growth quantile Sector
2002-01-31 0.2278331 1 Consumer Discretionary
sector_prtf[[1]][[1]][[2]][[1]]
Growth quantile Sector
2002-01-31 0.09700046 1 Consumer Staples
sector_prtf[[1]][[2]][[1]][[1]]
Growth quantile Sector
2003-01-31 -0.1081433 1 Consumer Discretionary
sector_prtf[[2]][[1]][[1]][[1]]
Growth quantile Sector
2002-02-28 0.3596547 1 Consumer Discretionary
The goal is to merge the lists together in a way that the sectors are together with the quantile and the corresponding startdate.
Growth quantile Sector
2002-01-31 0.2278331 1 Consumer Discretionary
Growth quantile Sector
2003-01-31 -0.1081433 1 Consumer Discretionary
Growth quantile Sector
2004-01-30 0.6446954 1 Consumer Discretionary
.
.
.
Growth quantile Sector
2017-01-31 0.1824898 1 Consumer Discretionary
Like I mentioned this should be done for every sector and every startdate.
I tried to merge the lists together by performing simple rbinds:
merged_sector <- lapply(sector_prtf, function(a) lapply(a, function(b) lapply(b, function(c) do.call("rbind", c))))
merged_sector <- lapply(merged_sector, function(a) lapply(a, function(b) do.call("rbind", b)))
merged_sector <- lapply(merged_sector, function(a) do.call("rbind", a))
merged_sector <- do.call("rbind", merged_sector)
After that the merged lists look like:
.
.
.
2012-01-319 -1.030502e-02 1 Materials
2012-01-3117 3.039239e-02 2 Materials
2012-01-3127 6.278972e-02 3 Materials
2012-01-3110 1.150880e-01 1 Real Estate
2012-01-3118 9.337119e-02 2 Real Estate
2012-01-3128 3.242025e-02 3 Real Estate
2012-01-3119 6.044756e-02 1 Utilities
2012-01-31110 1.154916e-01 2 Utilities
2012-01-3129 1.156366e-01 3 Utilities
2013-01-31 2.797345e-01 1 Consumer Discretionary
2013-01-311 1.875079e-01 2 Consumer Discretionary
2013-01-312 3.652037e-01 3 Consumer Discretionary
.
.
.
My idea was now to filter the merged DF by sector and quantile, but the dates are a huge problem (unique rownames).
Is there maybe a simpler way to solve this problem?
Thanks in advance
*Update: Here's a link to the requested dput
file. It includes only the first startmonth (January): https://ufile.io/y80fb
**Edit 2: I'm sorry for the inconvenience I caused with not providing a reproducible example.
list(list(list(structure(list(Growth = 0.227833070205427, quantile = structure(1L, .Label = "1", class = "factor"),
Sector = structure(1L, .Label = "Consumer Discretionary", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.00580189434527657, quantile = structure(1L, .Label = "2", class = "factor"),
Sector = structure(1L, .Label = "Consumer Discretionary", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.280654630370414, quantile = structure(1L, .Label = "3", class = "factor"),
Sector = structure(1L, .Label = "Consumer Discretionary", class = "factor")), class = "data.frame", row.names = "2002-01-31")),
list(structure(list(Growth = 0.0970004606893047, quantile = structure(1L, .Label = "1", class = "factor"),
Sector = structure(1L, .Label = "Consumer Staples", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.054821203483339, quantile = structure(1L, .Label = "2", class = "factor"),
Sector = structure(1L, .Label = "Consumer Staples", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.00837169953085215, quantile = structure(1L, .Label = "3", class = "factor"),
Sector = structure(1L, .Label = "Consumer Staples", class = "factor")), class = "data.frame", row.names = "2002-01-31")),
list(structure(list(Growth = -0.078767963284149, quantile = structure(1L, .Label = "1", class = "factor"),
Sector = structure(1L, .Label = "Energy", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = -0.069104950106169, quantile = structure(1L, .Label = "2", class = "factor"),
Sector = structure(1L, .Label = "Energy", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = -0.27207135756175, quantile = structure(1L, .Label = "3", class = "factor"),
Sector = structure(1L, .Label = "Energy", class = "factor")), class = "data.frame", row.names = "2002-01-31")),
list(structure(list(Growth = 0.009642535558954, quantile = structure(1L, .Label = "1", class = "factor"),
Sector = structure(1L, .Label = "Financials", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.0117244867054771, quantile = structure(1L, .Label = "2", class = "factor"),
Sector = structure(1L, .Label = "Financials", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = -0.185284889832411, quantile = structure(1L, .Label = "3", class = "factor"),
Sector = structure(1L, .Label = "Financials", class = "factor")), class = "data.frame", row.names = "2002-01-31")),
list(structure(list(Growth = 0.239390715659085, quantile = structure(1L, .Label = "1", class = "factor"),
Sector = structure(1L, .Label = "Health Care", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = -0.0162271493055311, quantile = structure(1L, .Label = "2", class = "factor"),
Sector = structure(1L, .Label = "Health Care", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = -0.067303679327545, quantile = structure(1L, .Label = "3", class = "factor"),
Sector = structure(1L, .Label = "Health Care", class = "factor")), class = "data.frame", row.names = "2002-01-31")),
list(structure(list(Growth = 0.0620349870410483, quantile = structure(1L, .Label = "1", class = "factor"),
Sector = structure(1L, .Label = "Industrials", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.0821803720980501, quantile = structure(1L, .Label = "2", class = "factor"),
Sector = structure(1L, .Label = "Industrials", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.137729664907273, quantile = structure(1L, .Label = "3", class = "factor"),
Sector = structure(1L, .Label = "Industrials", class = "factor")), class = "data.frame", row.names = "2002-01-31")),
list(structure(list(Growth = -0.0843930112785794, quantile = structure(1L, .Label = "1", class = "factor"),
Sector = structure(1L, .Label = "Information Technology", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = -0.172018997118367, quantile = structure(1L, .Label = "2", class = "factor"),
Sector = structure(1L, .Label = "Information Technology", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = -0.298718947065689, quantile = structure(1L, .Label = "3", class = "factor"),
Sector = structure(1L, .Label = "Information Technology", class = "factor")), class = "data.frame", row.names = "2002-01-31")),
list(structure(list(Growth = 0.0170747596874905, quantile = structure(1L, .Label = "1", class = "factor"),
Sector = structure(1L, .Label = "Materials", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.190415482682349, quantile = structure(1L, .Label = "2", class = "factor"),
Sector = structure(1L, .Label = "Materials", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.221341415148432, quantile = structure(1L, .Label = "3", class = "factor"),
Sector = structure(1L, .Label = "Materials", class = "factor")), class = "data.frame", row.names = "2002-01-31")),
list(structure(list(Growth = 0.168638361539387, quantile = structure(1L, .Label = "1", class = "factor"),
Sector = structure(1L, .Label = "Real Estate", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.0810611988754563, quantile = structure(1L, .Label = "2", class = "factor"),
Sector = structure(1L, .Label = "Real Estate", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.0365040437639329, quantile = structure(1L, .Label = "3", class = "factor"),
Sector = structure(1L, .Label = "Real Estate", class = "factor")), class = "data.frame", row.names = "2002-01-31")),
list(structure(list(Growth = 0.111350872628164, quantile = structure(1L, .Label = "1", class = "factor"),
Sector = structure(1L, .Label = "Utilities", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = -0.0978660942657028, quantile = structure(1L, .Label = "2", class = "factor"),
Sector = structure(1L, .Label = "Utilities", class = "factor")), class = "data.frame", row.names = "2002-01-31"),
structure(list(Growth = 0.112770511307641, quantile = structure(1L, .Label = "3", class = "factor"),
Sector = structure(1L, .Label = "Utilities", class = "factor")), class = "data.frame", row.names = "2002-01-31"))))