在R中将特定的列表列表合并在一起

时间:2018-11-21 20:29:27

标签: r

我有一个列表列表列表(我知道很多列表,总共大约6000个DataFrame)。 第一个列表指定开始月份(从1月到12月),第二个列表指定年份(2002年至2018年),第三个列表包含不同的部门(例如,全权委托或消费者主食,总计10个),最后一个包含分位数( 1至5)。
更清楚地说:sector_prtf[["StartMonth"]][["Year"]][["Sector"]][["Quantile"]]
这是DataFrames外观的示例:

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

目标是将列表与分位点和相应的开始日期合并在一起。

              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  

就像我提到的那样,应该对每个部门和每个开始日期进行此操作。

我尝试通过执行简单的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)  

之后,合并后的列表如下所示:

.
.
.
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
.
.
.  

我现在的想法是按扇区和分位数过滤合并的DF,但是日期是一个很大的问题(唯一的行名)。
有没有解决这个问题的简单方法? 预先感谢

*更新:这是指向所请求的dput文件的链接。它仅包括第一个开始月份(一月):https://ufile.io/y80fb

**编辑2:很抱歉,由于我没有提供可复制的示例而给您带来的不便。

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"))))

1 个答案:

答案 0 :(得分:1)

我写了一个递归Reduce,它应该做你想做的。因此,请仔细阅读?

假设您的列表列表名为ll

rReduce <- function(x) {
  y <- Reduce("rbind", x)
  if (is.list(y)) {
    return(rReduce(y))
  } else {
    return(x)
  }
}

res <- rReduce(ll)
print(res)
#                   Growth quantile                 Sector
#2002-01-31    0.227833070        1 Consumer Discretionary
#2002-01-311   0.097000461        1       Consumer Staples
#2002-01-312  -0.078767963        1                 Energy
#2002-01-313   0.009642536        1             Financials
#2002-01-314   0.239390716        1            Health Care
#2002-01-315   0.062034987        1            Industrials
#2002-01-316  -0.084393011        1 Information Technology
#...

这等效于

Reduce("rbind", Reduce("rbind", Reduce("rbind", ll)))

如果我没记错的话。现在,行名中仍然存在日期问题,但是可以通过类似以下方法轻松解决:

res$Date <- as.Date(substr(rownames(res), 1, 10))
rownames(res) <- NULL
print(res)
#         Growth quantile                 Sector       Date
#1   0.227833070        1 Consumer Discretionary 2002-01-31
#2   0.097000461        1       Consumer Staples 2002-01-31
#3  -0.078767963        1                 Energy 2002-01-31
#4   0.009642536        1             Financials 2002-01-31
#5   0.239390716        1            Health Care 2002-01-31
#6   0.062034987        1            Industrials 2002-01-31
#7  -0.084393011        1 Information Technology 2002-01-31
#8   0.017074760        1              Materials 2002-01-31

以上使用的数据是:

ll <- 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"))))