How to use select on multiple fields using dplyr

I have a character vector of the field names that I want to select using dplyr. I am using select _ () underscore string.

select(mtcars, mpg)                   # works OK
select(mtcars, mpg, disp, am)         # works OK for multiple fields

      

now use the underscore version

fie <- c("mpg")             
select_(mtcars, fie)                  # works OK for one
fie <- c("mpg", "disp", "am")
select_(mtcars, fie)                  # problem:  only returns one column
select_(mtcars, ~fie)                 # problem:  doesn't work

      

I am confused as to how to get this to work. Any suggestions? thank

+3


source to share


2 answers


If you are using select:



select(mtcars, one_of(fie))

      

+6


source


you must use the parameter of the .dots=

standard evaluation version of the function.



select_(mtcars, .dots=fie)

      

+6


source







All Articles