2

I have the code below, which I thought would let me merge several data frames together.

codes <- c('agglevel','industry','ownership','size','area')
ann2012full <- ann2012
for(i in 1:length(codes)){
eval(parse(text=paste('ann2012full <- left_join(ann2012full,
',codes[i],')', sep='')))
}

It seems like the first 3 files merge fine, and when I get the 4th file, I get this error message.

Error in left_join_impl(x, y, by$x, by$y, suffix$x, suffix$y, check_na_matches(na_matches)) : 
  Can't join on 'own_code' x 'own_code' because of incompatible types (integer / character)

I'm guessing there must be a way to coerce the integers into characters, and do the merge, right. I am just not sure how to get this to work. I was hoping for an elegant looping option, rather than doing this 5 separate and distinct ways. Thanks.

zx8754
  • 52,746
  • 12
  • 114
  • 209
ASH
  • 20,759
  • 19
  • 87
  • 200
  • 3
    See [this](https://stackoverflow.com/questions/8091303/simultaneously-merge-multiple-data-frames-in-a-list) – David Arenburg May 13 '18 at 19:32
  • 1
    Also @akrun's [answer](https://stackoverflow.com/questions/32066402/how-to-perform-multiple-left-joins-using-dplyr-in-r) – patL May 13 '18 at 19:42
  • Isn't there a way to get R to find common field names between different DFs and do the merge, rather then explicitly writing out how merge should be done? – ASH May 13 '18 at 19:45
  • 1
    `merge` does this anyway. It merges on common columns by default. – zx8754 May 13 '18 at 20:09
  • Ok, that makes sense. Everything is working now. Thanks guys! – ASH May 13 '18 at 20:46

0 Answers0