0

I am hoping to merge a number of data frames, one is called A, the other is called Comments_[[i]], and i ranges from 1 to 36. The name of the common column is called "Item.i.Q3", again i ranges from 1 to 36.

without a loop l I will do the following 36 times (changing i to a number ranged from 1 to 36) :

outcome <- merge(A , Comments_[[i]], by="Item.i.Q3", all= TRUE)

Was wondering if I can build a loop so I don't have to do it 36 times? Thanks :)

Syl
  • 29
  • 4
  • Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – Peter Apr 25 '21 at 14:26
  • Hm i might be missing something from that page you linked me, but I guess my question is I do not want to repeat the merge process 36 times, and so was hoping to see if I can build a loop to do the work. :) – Syl Apr 25 '21 at 14:29
  • Syl, to reiterate a couple points here, *we* are missing something here: reproducibility. As much as it sounds like we want everything handed to us, that's far from the truth: there are several components to problems like this that, lacking concrete samples, are left to our imagination or interpretation. While I suspect `Reduce(function(a,b) merge(a,b,by="Item.i.Q3",all.x=TRUE), mget(ls(pattern="Comments_")), init=A)` (very similar to @G.Grothendieck's answer) will work, it's difficult to know for sure without sample data. Thanks! – r2evans Apr 25 '21 at 15:06

1 Answers1

0

Construct a character vector of the names, df_names, and define the merge function needed, Merge. Then use mget to get the data fames themselves and use Reduce to iterate Merge over them. We assume that the common column to merge on is column number i in A and is 1 in all the other data frames.

# test data
i <- 1 # position of column in A to merge on; assume 1 for others
A <- Comments_1 <- Comments_2 <- BOD

df_names <- ls(pattern = "^Comments_")
Merge <- function(x, y) merge(x, y, by = 1, all = TRUE)
Reduce(Merge, mget(df_names), cbind(A[i], A[-i]))

giving:

  Time demand.x demand.y demand
1    1      8.3      8.3    8.3
2    2     10.3     10.3   10.3
3    3     19.0     19.0   19.0
4    4     16.0     16.0   16.0
5    5     15.6     15.6   15.6
6    7     19.8     19.8   19.8
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thanks - but the common columns are named differently in Comments_[[i]] - for Comments_[[1]], the common column is called Item.1.Q3; for Comments_[[2]], the common column is called Item.2.Q3 .. do you think there is a way to get around that please? :) – Syl Apr 25 '21 at 14:34
  • Thanks for replying again. I guess it is not in the same position in A, as a will have column Item.1.Q3 all the way to Item.36.Q3 :( – Syl Apr 25 '21 at 14:40
  • 1
    Have moved comments to answer. If this still does not reflect what you have then please provide a reproducible example which you should have done in the first place. – G. Grothendieck Apr 25 '21 at 14:49