Chapo Chapo - 1 year ago 85
R Question

Unused Argument Error in function during a do.call loop in R

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 !

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download