Dunois
Dunois

Reputation: 1843

Group dataframes stored in a list into separate, new dataframes

I have a list (dflist) that contains dataframes (dfX) that contain measurements for a collection of samples (e.g. samples 1-3; samp). Each dataframe itself contains measurements for a specific sample as measured using a specific instrument (e.g. instruments 1-3; inst). For example, dataframe 1 contains the measurements from instrument 1 for sample 1, dataframe 2 contains measurements from instrument 2 for sample 1, dataframe 3 contains measurements from instrument 1 for sample 3, and so on.

> 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

What I would like to do is loop through the list and merge dataframes containing measurements for the same sample (i.e., merge dfs by samp), to get an output as follows:

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

I believe the solution would involve merge and subset but I really have no clue how to do this, and I have hit a complete dead end as far as I am concerned.

Upvotes: 1

Views: 36

Answers (1)

twedl
twedl

Reputation: 1648

You can just put them all together with:

Reduce(rbind, dflist)

which gives:

   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

If you want to put it back into a list of dataframes separated by samples (although the full dataframe might be easier to work with imho):

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

Which gives you back a list of length two:

$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

Good luck!

Upvotes: 3

Related Questions