将列表列表转换为数据框 [英] Convert list of lists to dataframe

查看:101
本文介绍了将列表列表转换为数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个嵌套列表,名为mylist,其长度为4.

I got a nested list, named mylist which has length 4.

此列表中的每个元素都是实验:exp1.1exp1.2exp2.1exp2.2.

Each element of this list is an experiment: exp1.1, exp1.2, exp2.1 and exp2.2.

每个实验都包含四个植物生长阶段的长度(以天为单位):EM-V6 V6-R0 R0-R4R4-R9.

Each experiment contains observations of length (in days) of four plant growth stages: EM-V6 V6-R0 R0-R4 and R4-R9.

每个成长阶段都按照yearmean的数据框进行组织.

Each growth stage is organized as a data frame with year and mean.

这是完整的数据:

mylist=structure(list(exp1.1 = structure(list(`EM-V6` = structure(list(
    year = 2011:2100, mean = c(34, 34, 32, 28, 25, 32, 32, 28, 
    27, 30, 32, 31, 33, 28, 26, 31, 33, 27, 34, 26, 28, 27, 27, 
    30, 29, 31, 34, 30, 26, 31, 33, 33, 27, 30, 28, 32, 31, 29, 
    32, 31, 25, 28, 28, 26, 32, 29, 26, 31, 28, 29, 30, 25, 27, 
    32, 27, 28, 28, 30, 24, 30, 29, 29, 29, 28, 26, 28, 26, 26, 
    28, 31, 30, 27, 26, 28, 25, 24, 24, 30, 27, 26, 26, 27, 26, 
    26, 24, 26, 28, 25, 30, 26)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `V6-R0` = structure(list(year = 2011:2100, 
    mean = c(30, 33, 33, 32, 29, 30, 32, 31, 32, 30, 33, 30, 
    32, 33, 33, 32, 29, 31, 32, 28, 31, 29, 36, 29, 30, 30, 33, 
    31, 33, 30, 34, 32, 29, 31, 28, 30, 30, 29, 34, 31, 32, 31, 
    30, 28, 32, 29, 29, 32, 29, 28, 29, 29, 32, 31, 27, 32, 29, 
    31, 29, 29, 30, 29, 29, 29, 28, 28, 30, 30, 30, 32, 29, 29, 
    30, 29, 29, 29, 28, 28, 29, 30, 29, 29, 29, 30, 28, 30, 30, 
    29, 29, 29)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `R0-R4` = structure(list(year = 2011:2100, 
    mean = c(31, 32, 32, 33, 32, 32, 33, 31, 34, 32, 33, 33, 
    32, 31, 33, 31, 32, 32, 32, 30, 32, 31, 34, 30, 31, 32, 34, 
    33, 34, 32, 36, 33, 32, 32, 31, 30, 32, 32, 32, 32, 32, 32, 
    31, 30, 30, 31, 32, 32, 30, 30, 32, 31, 31, 32, 30, 32, 29, 
    32, 31, 30, 32, 30, 30, 31, 32, 30, 31, 30, 31, 32, 31, 31, 
    30, 30, 30, 31, 30, 30, 31, 30, 31, 30, 30, 30, 31, 32, 30, 
    31, 30, 30)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `R4-R9` = structure(list(year = 2011:2100, 
    mean = c(27, 29, 28, 28, 27, 30, 29, 27, 30, 26, 30, 28, 
    29, 28, 29, 27, 29, 28, 25, 26, 26, 25, 27, 27, 27, 28, 30, 
    28, 29, 27, 29, 28, 29, 28, 26, 26, 28, 28, 30, 28, 27, 25, 
    26, 25, 25, 26, 26, 27, 25, 25, 26, 25, 27, 28, 24, 27, 25, 
    28, 26, 24, 27, 26, 27, 25, 26, 26, 24, 26, 25, 26, 24, 25, 
    25, 26, 26, 25, 25, 25, 25, 25, 26, 25, 25, 25, 25, 26, 26, 
    26, 25, 24)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame")), .Names = c("EM-V6", "V6-R0", "R0-R4", 
"R4-R9")), exp1.2 = structure(list(`EM-V6` = structure(list(year = 2011:2100, 
    mean = c(34, 34, 32, 28, 25, 32, 32, 28, 27, 30, 32, 31, 
    33, 28, 26, 31, 33, 27, 34, 26, 28, 27, 27, 30, 29, 31, 34, 
    30, 26, 31, 33, 33, 27, 30, 28, 32, 31, 29, 32, 31, 25, 28, 
    28, 26, 32, 29, 26, 31, 28, 29, 30, 25, 27, 32, 27, 28, 28, 
    30, 24, 30, 29, 29, 29, 28, 26, 28, 26, 26, 28, 31, 30, 27, 
    26, 28, 25, 24, 24, 30, 27, 26, 26, 27, 26, 26, 24, 26, 28, 
    25, 30, 26)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `V6-R0` = structure(list(year = 2011:2100, 
    mean = c(30, 33, 33, 32, 29, 30, 32, 31, 32, 30, 33, 30, 
    32, 33, 33, 32, 29, 31, 32, 28, 31, 29, 36, 29, 30, 30, 33, 
    31, 33, 30, 34, 32, 29, 31, 28, 30, 30, 29, 34, 31, 32, 31, 
    30, 28, 32, 29, 29, 32, 29, 28, 29, 29, 32, 31, 27, 32, 29, 
    31, 29, 29, 30, 29, 29, 29, 28, 28, 30, 30, 30, 32, 29, 29, 
    30, 29, 29, 29, 28, 28, 29, 30, 29, 29, 29, 30, 28, 30, 30, 
    29, 29, 29)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `R0-R4` = structure(list(year = 2011:2100, 
    mean = c(31, 32, 32, 33, 32, 32, 33, 31, 34, 32, 33, 33, 
    32, 31, 33, 31, 32, 32, 32, 30, 32, 31, 34, 30, 31, 32, 34, 
    33, 34, 32, 36, 33, 32, 32, 31, 30, 32, 32, 32, 32, 32, 32, 
    31, 30, 30, 31, 32, 32, 30, 30, 32, 31, 31, 32, 30, 32, 29, 
    32, 31, 30, 32, 30, 30, 31, 32, 30, 31, 30, 31, 32, 31, 31, 
    30, 30, 30, 31, 30, 30, 31, 30, 31, 30, 30, 30, 31, 32, 30, 
    31, 30, 30)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `R4-R9` = structure(list(year = 2011:2100, 
    mean = c(27, 29, 28, 28, 27, 30, 29, 27, 30, 26, 30, 28, 
    29, 28, 29, 27, 29, 28, 25, 26, 26, 25, 27, 27, 27, 28, 30, 
    28, 29, 27, 29, 28, 29, 28, 26, 26, 28, 28, 30, 28, 27, 25, 
    26, 25, 25, 26, 26, 27, 25, 25, 26, 25, 27, 28, 24, 27, 25, 
    28, 26, 24, 27, 26, 27, 25, 26, 26, 24, 26, 25, 26, 24, 25, 
    25, 26, 26, 25, 25, 25, 25, 25, 26, 25, 25, 25, 25, 26, 26, 
    26, 25, 24)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame")), .Names = c("EM-V6", "V6-R0", "R0-R4", 
"R4-R9")), exp2.1 = structure(list(`EM-V6` = structure(list(year = 2011:2100, 
    mean = c(34, 34, 32, 28, 25, 32, 32, 28, 27, 30, 32, 31, 
    33, 28, 26, 31, 33, 27, 34, 26, 28, 27, 27, 30, 29, 31, 34, 
    30, 26, 31, 33, 33, 27, 30, 28, 32, 31, 29, 32, 31, 25, 28, 
    28, 26, 32, 29, 26, 31, 28, 29, 30, 25, 27, 32, 27, 28, 28, 
    30, 24, 30, 29, 29, 29, 28, 26, 28, 26, 26, 28, 31, 30, 27, 
    26, 28, 25, 24, 24, 30, 27, 26, 26, 27, 26, 26, 24, 26, 28, 
    25, 30, 26)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `V6-R0` = structure(list(year = 2011:2100, 
    mean = c(30, 33, 33, 32, 29, 30, 32, 31, 32, 30, 33, 30, 
    32, 33, 33, 32, 29, 31, 32, 28, 31, 29, 36, 29, 30, 30, 33, 
    31, 33, 30, 34, 32, 29, 31, 28, 30, 30, 29, 34, 31, 32, 31, 
    30, 28, 32, 29, 29, 32, 29, 28, 29, 29, 32, 31, 27, 32, 29, 
    31, 29, 29, 30, 29, 29, 29, 28, 28, 30, 30, 30, 32, 29, 29, 
    30, 29, 29, 29, 28, 28, 29, 30, 29, 29, 29, 30, 28, 30, 30, 
    29, 29, 29)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `R0-R4` = structure(list(year = 2011:2100, 
    mean = c(31, 32, 32, 33, 32, 32, 33, 31, 34, 32, 33, 33, 
    32, 31, 33, 31, 32, 32, 32, 30, 32, 31, 34, 30, 31, 32, 34, 
    33, 34, 32, 36, 33, 32, 32, 31, 30, 32, 32, 32, 32, 32, 32, 
    31, 30, 30, 31, 32, 32, 30, 30, 32, 31, 31, 32, 30, 32, 29, 
    32, 31, 30, 32, 30, 30, 31, 32, 30, 31, 30, 31, 32, 31, 31, 
    30, 30, 30, 31, 30, 30, 31, 30, 31, 30, 30, 30, 31, 32, 30, 
    31, 30, 30)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `R4-R9` = structure(list(year = 2011:2100, 
    mean = c(27, 29, 28, 28, 27, 30, 29, 27, 30, 26, 30, 28, 
    29, 28, 29, 27, 29, 28, 25, 26, 26, 25, 27, 27, 27, 28, 30, 
    28, 29, 27, 29, 28, 29, 28, 26, 26, 28, 28, 30, 28, 27, 25, 
    26, 25, 25, 26, 26, 27, 25, 25, 26, 25, 27, 28, 24, 27, 25, 
    28, 26, 24, 27, 26, 27, 25, 26, 26, 24, 26, 25, 26, 24, 25, 
    25, 26, 26, 25, 25, 25, 25, 25, 26, 25, 25, 25, 25, 26, 26, 
    26, 25, 24)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame")), .Names = c("EM-V6", "V6-R0", "R0-R4", 
"R4-R9")), exp2.2 = structure(list(`EM-V6` = structure(list(year = 2011:2100, 
    mean = c(34, 34, 32, 28, 25, 32, 32, 28, 27, 30, 32, 31, 
    33, 28, 26, 31, 33, 27, 34, 26, 28, 27, 27, 30, 29, 31, 34, 
    30, 26, 31, 33, 33, 27, 30, 28, 32, 31, 29, 32, 31, 25, 28, 
    28, 26, 32, 29, 26, 31, 28, 29, 30, 25, 27, 32, 27, 28, 28, 
    30, 24, 30, 29, 29, 29, 28, 26, 28, 26, 26, 28, 31, 30, 27, 
    26, 28, 25, 24, 24, 30, 27, 26, 26, 27, 26, 26, 24, 26, 28, 
    25, 30, 26)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `V6-R0` = structure(list(year = 2011:2100, 
    mean = c(30, 33, 33, 32, 29, 30, 32, 31, 32, 30, 33, 30, 
    32, 33, 33, 32, 29, 31, 32, 28, 31, 29, 36, 29, 30, 30, 33, 
    31, 33, 30, 34, 32, 29, 31, 28, 30, 30, 29, 34, 31, 32, 31, 
    30, 28, 32, 29, 29, 32, 29, 28, 29, 29, 32, 31, 27, 32, 29, 
    31, 29, 29, 30, 29, 29, 29, 28, 28, 30, 30, 30, 32, 29, 29, 
    30, 29, 29, 29, 28, 28, 29, 30, 29, 29, 29, 30, 28, 30, 30, 
    29, 29, 29)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `R0-R4` = structure(list(year = 2011:2100, 
    mean = c(31, 32, 32, 33, 32, 32, 33, 31, 34, 32, 33, 33, 
    32, 31, 33, 31, 32, 32, 32, 30, 32, 31, 34, 30, 31, 32, 34, 
    33, 34, 32, 36, 33, 32, 32, 31, 30, 32, 32, 32, 32, 32, 32, 
    31, 30, 30, 31, 32, 32, 30, 30, 32, 31, 31, 32, 30, 32, 29, 
    32, 31, 30, 32, 30, 30, 31, 32, 30, 31, 30, 31, 32, 31, 31, 
    30, 30, 30, 31, 30, 30, 31, 30, 31, 30, 30, 30, 31, 32, 30, 
    31, 30, 30)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame"), `R4-R9` = structure(list(year = 2011:2100, 
    mean = c(27, 29, 28, 28, 27, 30, 29, 27, 30, 26, 30, 28, 
    29, 28, 29, 27, 29, 28, 25, 26, 26, 25, 27, 27, 27, 28, 30, 
    28, 29, 27, 29, 28, 29, 28, 26, 26, 28, 28, 30, 28, 27, 25, 
    26, 25, 25, 26, 26, 27, 25, 25, 26, 25, 27, 28, 24, 27, 25, 
    28, 26, 24, 27, 26, 27, 25, 26, 26, 24, 26, 25, 26, 24, 25, 
    25, 26, 26, 25, 25, 25, 25, 25, 26, 25, 25, 25, 25, 26, 26, 
    26, 25, 24)), .Names = c("year", "mean"), row.names = c(NA, 
-90L), class = "data.frame")), .Names = c("EM-V6", "V6-R0", "R0-R4", 
"R4-R9"))), .Names = c("exp1.1", "exp1.2", "exp2.1", "exp2.2"
))

我需要做的是将这个嵌套列表取消列出"到看起来像这样的数据框中:

What I need to do is to "unlist" this nested list to a data frame that will look like this:

YEAR   EXP   EM-V6   V6-R0   R0-R4   R4-R9
2011  exp1.1  34      30      31      27
2011  exp1.2  34      30      31      27
2011  exp2.1  34      30      31      27
2011  exp1.1  34      30      31      27

意思是:

 - first year, first experiment, and growth stages.
 - first year, second experiment and growth stages.
 - first year, third experiment and growth stages
 - first year, fourth experiment and growth stages
 - second year, first experiment and growth stages

以此类推.

如何执行数据转换?

推荐答案

使用两次data.table -package中的rbindlist的替代方法:

An alternative using rbindlist from the data.table-package twice:

library(data.table)
# bind the dataframes in the 'listed lists' together and include the year with the 'id'-parameter
# the resulting 'data.table's are returned as a list
step1 <- lapply(mylist, rbindlist, id = 'stages')
# bind the resulting list together and include the experiment id
step2 <- rbindlist(step1, id = 'experiment')
# reshape to wide format
dcast(step2, year + experiment ~ stages, value.var = 'mean')

或者一口气:

dcast(rbindlist(lapply(mylist, rbindlist, id = 'stages'), id = 'experiment'),
      year + experiment ~ stages, value.var = 'mean')

给出:

     year experiment EM-V6 R0-R4 R4-R9 V6-R0
  1: 2011     exp1.1    34    31    27    30
  2: 2011     exp1.2    34    31    27    30
  3: 2011     exp2.1    34    31    27    30
  4: 2011     exp2.2    34    31    27    30
  5: 2012     exp1.1    34    32    29    33
 ---                                        
356: 2099     exp2.2    30    30    25    29
357: 2100     exp1.1    26    30    24    29
358: 2100     exp1.2    26    30    24    29
359: 2100     exp2.1    26    30    24    29
360: 2100     exp2.2    26    30    24    29

这篇关于将列表列表转换为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆