with - r format data frame




Converti un elenco di liste in un dataframe (2)

Possiamo usare il tidyverse con un codice più compatto e leggibile

library(dplyr)
library(tidyr)
library(purrr)
res1 <- mylist %>%
            #bind the inner datasets and create an id column
            map(bind_rows, .id = "id") %>%
            #bind the outer datasets and create an EXP column
            bind_rows(.id = "EXP") %>% 
            #reshape to wide format
            spread(id, mean) 

head(res1, 4)
#     EXP year EM-V6 R0-R4 R4-R9 V6-R0
#1 exp1.1 2011    34    31    27    30
#2 exp1.1 2012    34    32    29    33
#3 exp1.1 2013    32    32    28    33
#4 exp1.1 2014    28    33    28    32

Oppure possiamo avvicinarci a questo facendo un ciclo attraverso la mylist con lapply , quindi creare una nuova colonna 'name' usign Map cbind i names degli elementi della list interna, quindi rbind gli elementi della list con do.call(rbind , ora fai un secondo Map per creare una nuova colonna in base ai names di " rbind list ", rbind gli elementi della list e poi reshape dalla base R per convertirla in "wide"

res <- do.call(rbind, Map(cbind, lapply(mylist, function(x) 
    do.call(rbind, Map(cbind, x, name = names(x)))), EXP= names(mylist)))
res2 <- reshape(res, idvar = c("year", "EXP"), 
              timevar = "name", direction = "wide")
row.names(res2) <- NULL
head(res2, 4)
#   year    EXP mean.EM-V6 mean.V6-R0 mean.R0-R4 mean.R4-R9
#1 2011 exp1.1         34         30         31         27
#2 2012 exp1.1         34         33         32         29
#3 2013 exp1.1         32         33         32         28
#4 2014 exp1.1         28         32         33         28

NOTA: nessun pacchetto esterno utilizzato (100% base R )

oppure usa dcast da reshape2 per trasformarlo in formato 'wide'

library(reshape2)
res2 <- dcast(res, year + EXP~name, value.var = "mean") 
head(res2, 4)
#   year    EXP EM-V6 V6-R0 R0-R4 R4-R9
#1 2011 exp1.1    34    30    31    27
#2 2011 exp1.2    34    30    31    27
#3 2011 exp2.1    34    30    31    27
#4 2011 exp2.2    34    30    31    27

Ho una lista annidata, chiamata mylist che ha la lunghezza 4.

Ogni elemento di questo elenco è un esperimento: exp1.1 , exp1.2 , exp2.1 ed exp2.2 .

Ogni esperimento contiene osservazioni di lunghezza (in giorni) di quattro fasi di crescita della pianta: EM-V6 V6-R0 R0-R4 e R4-R9 .

Ogni fase di crescita è organizzata come un frame di dati con year e mean .

Ecco i dati completi:

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

Quello che devo fare è "non elencare" questa lista annidata in una cornice dati che assomiglierà a questa:

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

Che significa:

 - 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

e così via.

Come eseguire quella trasformazione dei dati?


Tidyverse alternativo:

library(tidyverse)

map_df(mylist, ~bind_rows(., .id="id"), .id="EXP") %>% 
  spread(id, mean)




type-conversion