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
subset
really shouldn't be used for these types of things. From the help page
This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like [, and in particular the non-standard evaluation of argument subset can have unanticipated consequences.
For your particular problem I don't see why just replacing subset with directly using "[" would be a problem.
rcombine <- function(List, Vars) {
List2 <- lapply(List, "[", i= , j = Vars, drop = FALSE) # here is the change
Reduce(rbind, List2)
}
# alternatively...
rcombine <- function(List, Vars) {
List2 <- lapply(List, function(x){x[, Vars, drop = FALSE]}) # here is the change
Reduce(rbind, List2)
}
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'))