0

I am trying to merge data frames consecutively to one data frame in r. See the code below. Is there a simpler way of doing this. This whole process has to be repeated for Feb2016, March2016 and so on

Jan2016<-Jan2016%>%left_join(djan16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dfeb16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dmar16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dapr16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dmay16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(djun16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(djul16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(daug16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dsep16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(doct16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dnov16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(ddec16, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
  
Jan2016<-Jan2016%>%left_join(djan17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dfeb17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dmar17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dapr17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dmay17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(djun17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(djul17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(daug17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dsep17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(doct17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(dnov17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Jan2016<-Jan2016%>%left_join(ddec17, by=c("ACCOUNT_ID" = "ACCOUNT_ID"))
Manfred
  • 103
  • 1
  • 8
  • 1
    Something like `Reduce(function(a,b) left_join(a, b, by=c(...)), list(Jan2016, djan16, dfeb16, ...))`. You might be able to use `mget(ls(pattern="^d...1[67]$"))` for the second argument. – r2evans Nov 16 '20 at 16:51
  • ... and ultimately by https://stackoverflow.com/questions/8091303/simultaneously-merge-multiple-data-frames-in-a-list – r2evans Nov 16 '20 at 17:01
  • BTW: your previous question from 2+ hours ago was closed as a duplicate of another question that is very similar to this, and the answers on that question (imo) were clear in how to approach this. If you disagree, that is fine, but ... posting the same question with the same (unreproducible) code with nothing else is wrong on SO. I'll give you some time to [edit] your question and explain what does not work in the suggested duplicate ... otherwise, this too will be closed. – r2evans Nov 16 '20 at 17:11
  • Thank you that works very well. How would the code be enhanced create a list for Jan2016, Feb2016 (this is the a data set – Manfred Nov 16 '20 at 17:19
  • 1
    Probably better managed with a [list of frames](https://stackoverflow.com/a/24376207/3358227), though I don't know if that helps you get to that point. If your RHS frames are always the same (it is unclear), then `lapply(ls(pattern="^[A-Z].*20[0-9]{2}$"), function(frm) Reduce(function(a,b) left_join(...), ls(...)))` should return a list of frames, all joined up. – r2evans Nov 16 '20 at 17:28
  • 1
    Many thanks for this information. It has been very useful – Manfred Nov 16 '20 at 17:33

0 Answers0