0
funMergeA <- function(x,y = NULL) {
    if (is.null(y)) {
        return(x)
    } else {
        return(funMerge(x,y,"a"))
    }
}

funMerge <- function(x,y,myby="a") {
    return(merge.data.frame(x,y,by=myby,all=TRUE))
}

b <- list(data.frame(a=1:10,b=rnorm(10)),
          data.frame(a=1:10,b=rnorm(10)),
          data.frame(a=1:10,b=rnorm(10)))

this works :

funMergeA(funMergeA(funMergeA(b[[1]],NULL),b[[2]]),b[[3]])

this doesn't :

do.call(funMergeA,b)

I am confused as to why the second one doesn't work. My understanding is that the 2 expressions are strictly equivalent. Is my understanding flawed ?

All help welcome !

Chapo
  • 2,563
  • 3
  • 30
  • 60
  • `do.call(funMergeA,b)` is the same as `funMergeA(b[[1]], b[[2]], b[[3]])`, so your understand is flawed. – Roland Jul 26 '17 at 10:43
  • 1
    You need `Reduce(funMergeA,b)` - do.call works through b one at a time but is not recursive in the way that you need it to be. – Andrew Gustar Jul 26 '17 at 10:43
  • 1
    Thank you @Andrew Gustar for the helpful tip. If you want to post an answer i'll validate it. – Chapo Jul 26 '17 at 23:11

1 Answers1

2

do.call iterates through b one element at a time but is not recursive in the way that you need it to be. You need Reduce(funMergeA, b).

Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32