I am writing a wrapper to combine any number of datasets row-wise. Since some may have unique variables, I am first restricting to the variables in the data.
My function works like this
rcombine <- function(List, Vars) {
List2 <- lapply(List, subset, select=Vars)
Reduce(rbind, List2)
}
When I run the code directly, it works. But in the function, my variable Vars
disappears.
For instance:
x <- data.frame('a'=sample(LETTERS, 10), 'b'=sample(LETTERS, 10), 'c'=sample(LETTERS, 10))
y <- data.frame('a'=sample(LETTERS, 10), 'b'=sample(LETTERS, 10), 'e'=sample(LETTERS, 10))
rcombine(list(x, y), c('a', 'b'))
gives me:
Error in eval(expr, envir, enclos) : object 'Vars' not found
but running:
List <- list(x, y)
Reduce(rbind, lapply(List, subset, select=c('a','b')))
Works. I can print Vars
from the function, but inside lapply
it disappears. What is going on?