How to remove a dataframe column using a subset function and a variable?

I have a dataframe with a bunch of columns. I would like to delete one of the columns named age ..

type.name <- "age" 

so i tried

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

but it throws an error: invalid argument for unary operator.

However it works

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

How can I make dynamically work with a variable?

+4
source share
1 answer

He works with

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

and

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

and

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

Source: https://habr.com/ru/post/1446002/


All Articles