I have a df as follows:
a <- data_frame(keep=c("hello", "world"),drop = c("nice", "work"))
a
Source: local data frame [2 x 2]
keep drop
(chr) (chr)
1 hello nice
2 world work
I can use a %>% select(-drop)
to drop the column without problem. however, if I want to pass a variable to present drop
column, then it returns error.
name <- "drop"
a %>% select(-(name))
Error in -(name) : invalid argument to unary operator
Best Answer
You can use
one_of
to find the column positions and then use-
to drop it,select(-one_of(name))
, if you check?select
, the usage is documented in the Drop variable section in the Examples:Or with
select_
, you need to paste-
with the column names to drop them and pass the pasted column names to the.dots
parameter if there are more than one column to be dropped: