You can define the names of the columns you want inside [
(see the help file ?Extract
or help("[")
for the subset operator [
).
testdf[ names(testdf)[!names(testdf) %in% varnames] ]
## or
## testdf[, names(testdf)[!names(testdf) %in% varnames] , drop = FALSE]
Or, more concisely (thanks @Frank)
testdf[ setdiff(names(testdf), varnames)]
var3
1 1
2 1
3 1
4 1
where
names(testdf)
# [1] "var1" "var2" "var3"
varnames
# [1] "var1" "var2"
And So
names(testdf) %in% varnames
# [1] TRUE TRUE FALSE
And therefore
names(testdf)[!names(testdf) %in% varnames]
# [1] "var3"
Which is the same as
testdf[, "var3" ]
And drop = FALSE
to stop it 'dropping' to a vector if there's only one column returned.
Also, if you look at the help file for lapply(X, FUN, ...)
?lapply
lapply returns a list of the same length as X
This is why you're getting a list.
As a bonus - can someone tell me why it is ever useful for lapply to return this nested named list instead of simple vector? It seems very different than, for instance, Python. Thank you.
When you're working with a list, and you want it to remain as a list.