【问题标题】:Group dataframes stored in a list into separate, new dataframes将存储在列表中的数据帧分组为单独的新数据帧
【发布时间】:2018-12-19 22:54:09
【问题描述】:

我有一个列表 (dflist),其中包含数据帧 (dfX),其中包含样本集合的测量值(例如样本 1-3;samp)。每个数据框本身都包含使用特定仪器测量的特定样本的测量值(例如仪器 1-3;inst)。例如,数据框 1 包含来自仪器 1 的样本 1 的测量值,数据框 2 包含来自仪器 2 的样本 1 的测量值,数据框 3 包含来自仪器 1 的样本 3 的测量值,依此类推。

> a1 <- c('a1', 'b1', 'c1')
> a2 <- c('a2', 'b2', 'c2')
> a3 <- c('a3', 'b3', 'c3')
> a4 <- c('a4', 'b4', 'c4')
> b1 <- c(1:3)
> b2 <- c(4:6)
> b3 <- c(7:9)
> b4 <- c(10:12)
> c1 <- c('samp1', 'samp1', 'samp1')
> c2 <- c('samp1', 'samp1', 'samp1')
> c3 <- c('samp2', 'samp2', 'samp2')
> c4 <- c('samp2', 'samp2', 'samp2')
> d1 <- c('inst1', 'inst1', 'inst1')
> d2 <- c('inst2', 'inst2', 'inst2')
> d3 <- c('inst1', 'inst1', 'inst1')
> d4 <- c('inst2', 'inst2', 'inst2')
> df1 <- data.frame(a1, b1, c1, d1)
> df2 <- data.frame(a2, b2, c2, d2)
> df3 <- data.frame(a3, b3, c3, d3)
> df4 <- data.frame(a4, b4, c4, d4)
> nams <- c('Reads', 'Mean_Val', 'Samp', 'Inst')
> dflist <- list(df1, df2, df3, df4)
> dflist <- lapply(dflist, setNames, nm=nams)
> dflist
[[1]]
  Reads Mean_Val  Samp  Inst
1    a1        1 samp1 inst1
2    b1        2 samp1 inst1
3    c1        3 samp1 inst1

[[2]]
  Reads Mean_Val  Samp  Inst
1    a2        4 samp1 inst2
2    b2        5 samp1 inst2
3    c2        6 samp1 inst2

[[3]]
  Reads Mean_Val  Samp  Inst
1    a3        7 samp2 inst1
2    b3        8 samp2 inst1
3    c3        9 samp2 inst1

[[4]]
  Reads Mean_Val  Samp  Inst
1    a4       10 samp2 inst2
2    b4       11 samp2 inst2
3    c4       12 samp2 inst2

我想做的是遍历列表并合并包含同一样本测量值的数据帧(即,将dfs 与samp 合并),以获得如下输出:

[[1]]
  Reads Mean_Val  Samp  Inst
1    a1        1 samp1 inst1
2    b1        2 samp1 inst1
3    c1        3 samp1 inst1
4    a2        4 samp1 inst2
5    b2        5 samp1 inst2
6    c2        6 samp1 inst2

[[2]]
  Reads Mean_Val  Samp  Inst
1    a3        7 samp2 inst1
2    b3        8 samp2 inst1
3    c3        9 samp2 inst1
4    a4       10 samp2 inst2
5    b4       11 samp2 inst2
6    c4       12 samp2 inst2

我相信解决方案将涉及mergesubset,但我真的不知道如何做到这一点,而且就我而言,我已经陷入了完全的死胡同。

【问题讨论】:

    标签: r list dataframe grouping lapply


    【解决方案1】:

    您可以将它们全部放在一起:

    Reduce(rbind, dflist)
    

    给出:

       Reads Mean_Val  Samp  Inst
    1     a1        1 samp1 inst1
    2     b1        2 samp1 inst1
    3     c1        3 samp1 inst1
    4     a2        4 samp1 inst2
    5     b2        5 samp1 inst2
    6     c2        6 samp1 inst2
    7     a3        7 samp2 inst1
    8     b3        8 samp2 inst1
    9     c3        9 samp2 inst1
    10    a4       10 samp2 inst2
    11    b4       11 samp2 inst2
    12    c4       12 samp2 inst2
    

    如果您想将其放回由样本分隔的数据框列表中(尽管完整的数据框可能更容易使用恕我直言):

    df <- Reduce(rbind, dflist)
    split(df, df$Samp)
    

    这会给你一个长度为 2 的列表:

    $samp1
      Reads Mean_Val  Samp  Inst
    1    a1        1 samp1 inst1
    2    b1        2 samp1 inst1
    3    c1        3 samp1 inst1
    4    a2        4 samp1 inst2
    5    b2        5 samp1 inst2
    6    c2        6 samp1 inst2
    
    $samp2
       Reads Mean_Val  Samp  Inst
    7     a3        7 samp2 inst1
    8     b3        8 samp2 inst1
    9     c3        9 samp2 inst1
    10    a4       10 samp2 inst2
    11    b4       11 samp2 inst2
    12    c4       12 samp2 inst2
    

    祝你好运!

    【讨论】:

    • 完成同样事情的整洁/dplyr 方式是dflist %&gt;% bind_rows() %&gt;% split(.$Samp)
    猜你喜欢
    • 2019-10-25
    • 2018-03-03
    • 2023-02-09
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2022-01-07
    • 2017-09-26
    • 2021-07-25
    相关资源
    最近更新 更多