Rookie question - thanks in advance for patience...
I have a dataframe:
vals <- c(1,1,1,1)
testdf <- data.frame("var1"=vals, "var2"=vals, "var3"=vals)
I have a character vector of variable names:
varnames <- c("var1", "var2")
This is a character vector b/c I use it to generate a formula earlier in the script.
I'd like to subset a dataframe such that variables in varnames are excluded, e.g.
newDF <- subset(df, select=-varnames)
This creates an error since subset
expects names instead of characters. So, I use lapply
to change the characters to names:
varnames <- lapply(varnames, as.name)
The result of this lapply function is a named(?) and nested(?) list.
[[1]]
var1
[[2]]
var2
[[3]]
var3
Here's where I get lost (I feel like Mugatu on crazy pills... is this confusing to anyone else!?). I can see that each value has correctly been changed from character to name, but it's in this weird nested structure - so when I try to subset, I get an error.
I've tried various solutions to unnest and unname, but with no success. This must be something easy I'm missing.
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.