0

Say, I have a list of data frames like this:

# To create dummy data
df_create <- function(x) data.frame(a = 1:x, b = runif(x))

# Create list of dummy data frames
mylist <- list(df_create(10), df_create(10), df_create(10), df_create(10))

I can bind them together using cbind and do.call, like this:

# Bind data frames together
do.call(cbind, mylist)

#    a          b  a          b  a         b  a          b
#1   1 0.02000419  1 0.29098648  1 0.1433533  1 0.59307504
#2   2 0.26775702  2 0.23208306  2 0.1729216  2 0.04506591
#3   3 0.01336910  3 0.78083216  3 0.6713367  3 0.84223524
#4   4 0.46972923  4 0.43417273  4 0.2663393  4 0.03955171
#5   5 0.62543992  5 0.38561626  5 0.3620604  5 0.11384484
#6   6 0.25747343  6 0.53876212  6 0.2875563  6 0.10921397
#7   7 0.48790572  7 0.08488719  7 0.1627827  7 0.08641714
#8   8 0.54707416  8 0.28780866  8 0.7763911  8 0.48040182
#9   9 0.58214358  9 0.98220356  9 0.5845031  9 0.80809334
#10 10 0.11839613 10 0.40025797 10 0.7633499 10 0.86299285

But this leaves all the a columns, which are the same. So, what I really need to do is to join them. If I was to do just the first couple, it might look like this:

# Just two elements
left_join(mylist[[1]], mylist[[2]], by = "a")

#    a        b.x        b.y
#1   1 0.02000419 0.29098648
#2   2 0.26775702 0.23208306
#3   3 0.01336910 0.78083216
#4   4 0.46972923 0.43417273
#5   5 0.62543992 0.38561626
#6   6 0.25747343 0.53876212
#7   7 0.48790572 0.08488719
#8   8 0.54707416 0.28780866
#9   9 0.58214358 0.98220356
#10 10 0.11839613 0.40025797

which is great. That's what I want, but for all the data frames in the list. So, next I try to combine left_join with do.call:

# Expanding using do.call
do.call(left_join, c(mylist, by = "a"))

This throws the following error:

Error: suffix must be a character vector of length 2, not list of length 2

This seems to be referring to the suffix that is added to duplicate columns (e.g., the .x and .y added to my b columns in the two data frame join example above). As I'm not specifying suffix anywhere, it must be that default parameter values for left_join are not playing well with do.call, but I don't see how to resolve this error. Any suggestions?

Sotos
  • 51,121
  • 6
  • 32
  • 66
Dan
  • 11,370
  • 4
  • 43
  • 68
  • 1
    You mean `Reduce(function(x, y)left_join(x, y, by = 'a'), mylist)`? – Sotos Dec 15 '17 at 13:11
  • @Sotos You made that look easy! Thanks a lot, Sotos. I *always* forget about `Reduce`... Can you add that as an answer so I can mark it correct? – Dan Dec 15 '17 at 13:13

0 Answers0