I have lists of variable length with dataframes. I want to merge the dfs in each list into a single df using a specified column name or index that varies by df. Here's an example with 3 dfs
my.list <- list(
data.frame(a = 1:10, b = letters[1:10], c = 101:110),
data.frame(d = 6:15, e = letters[1:10], f = 1:10),
data.frame(l = 2:11, m = letters[11:20], o = 1:10))
and I want to merge by a specific column of each df mentioned in ids
ids <- c('a', 'f', 'l')
to get something that looks like
id b c d e m o
1 a 101 6 a NA NA
2 b 102 7 b k 1
3 c 103 8 c l 2
4 d 104 9 d m 3
5 e 105 10 e n 4
6 f 106 11 f o 5
7 g 107 12 g p 6
8 h 108 13 h q 7
9 i 109 14 i r 8
10 j 110 15 j s 9
11 NA NA NA NA t 10
I've tried to do this with merge
and/or Reduce
, but failed to pass on the ids