R – How to drop a dataframe column with the subset function and a variable

r

I have a dataframe with a bunch of columns. I'd like to drop one of the columns named age..

type.name <- "age"

so I tried

df <- subset(df, select = -type.name)

but it throws the error: invalid argument to unary operator.

However, this does work

df <- subset(df, select = -age)

How do I do get it to work dynamically with the variable?

Best Answer

It works with

subset(df, select = -eval(parse(text=type.name)))

and

subset(df, select = names(df) != type.name)

and

"[[<-"(df, type.name, value = NULL)