Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove columns the tidyeval way

I would like to remove a vector of columns using dplyr >= 0.7

library(dplyr)
data(mtcars)

rem_cols <- c("wt", "qsec", "vs", "am", "gear", "carb")
head(select(mtcars, !!paste0("-", rem_cols)))

Error: Strings must match column names. Unknown columns: -wt, -qsec, -vs, -am, -gear, -carb

dplyr < 0.7 worked as follows:

head(select_(mtcars, .dots = paste0("-", rem_cols)))
#                    mpg cyl disp  hp drat
# Mazda RX4         21.0   6  160 110 3.90
# Mazda RX4 Wag     21.0   6  160 110 3.90
# Datsun 710        22.8   4  108  93 3.85
# Hornet 4 Drive    21.4   6  258 110 3.08
# Hornet Sportabout 18.7   8  360 175 3.15
# Valiant           18.1   6  225 105 2.76

I've tried about all combinations of rlang:syms(), !!, !!!, quo and enquo that I can think of... help!?

like image 885
Scott Avatar asked Jul 14 '17 10:07

Scott


1 Answers

We can use one_of

 mtcars %>%
        select(-one_of(rem_cols))
like image 115
akrun Avatar answered Nov 14 '22 21:11

akrun