Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use select for multiple fields using dplyr

Tags:

r

dplyr

I have a character vector of field names that I want to select using dplyr. I'm using the underscore version of select_().

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

now let's 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'm confused as to how to get this to work. Any suggestions? Thanks

like image 523
hackR Avatar asked Dec 06 '22 21:12

hackR


1 Answers

If you use select:

select(mtcars, one_of(fie))
like image 106
Shenglin Chen Avatar answered Jan 13 '23 14:01

Shenglin Chen