select columns based on multiple strings with dplyr contains()

前端 未结 1 1797
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 05:14

I want to select multiple columns based on their names with a regex expression. I am trying to do it with the piping syntax of the dplyr package. I che

相关标签:
1条回答
  • 2020-12-01 05:39

    You can use matches

     mtcars %>%
            select(matches('m|ar')) %>%
            head(2)
     #              mpg am gear carb
     #Mazda RX4      21  1    4    4
     #Mazda RX4 Wag  21  1    4    4
    

    According to the ?select documentation

    ‘matches(x, ignore.case = TRUE)’: selects all variables whose name matches the regular expression ‘x’

    Though contains work with a single string

    mtcars %>% 
           select(contains('m'))
    
    0 讨论(0)
提交回复
热议问题