how can i tell select() in dplyr that the string it is seeing is a column name in a data frame

后端 未结 8 789
感动是毒
感动是毒 2020-12-24 11:15

I tried searching but didn\'t find an answer to this question.

I\'m trying to use the select statement in dplyr but am having problems when I try to send it strings

相关标签:
8条回答
  • 2020-12-24 11:50

    For strings you can do

    my_cols <- c("mpg", "disp")
    mtcars %>% select(!!!my_cols)
    

    although, I think it would probably be better practice to avoid strings and do

    my_cols <- quos(mpg, disp)
    mtcars %>% select(!!!my_cols)
    
    0 讨论(0)
  • 2020-12-24 11:53

    In more recent versions of dplyr, this is possible in select with one_of, as in

    my_cols <- c('mpg', 'disp')
    mtcars %>% select(one_of(my_cols))
    
    0 讨论(0)
提交回复
热议问题