dplyr: select columns by position in NSE

早过忘川 提交于 2019-12-01 15:37:53

You can just use select with a numeric vector of indexes:

positions <- c(1,28:31)
DF %>% select(positions)

Using mtcars and:

  1. add auto as my first column
  2. select `c(auto, the first column, and any column containing an 'a')
  3. subset the data where only the sums of numeric rows are greater than the mean of all sums of those rows

mtcars %>% mutate(auto = row.names(.)) %>% select(auto, 1, contains('a')) %>% dplyr::filter(rowSums(.[-1]) > mean(rowSums(.[-1])))

             auto  mpg drat am gear carb
1       Mazda RX4 21.0 3.90  1    4    4
2   Mazda RX4 Wag 21.0 3.90  1    4    4
3      Datsun 710 22.8 3.85  1    4    1
4       Merc 240D 24.4 3.69  0    4    2
5        Merc 230 22.8 3.92  0    4    2
6        Merc 280 19.2 3.92  0    4    4
7        Fiat 128 32.4 4.08  1    4    1
8     Honda Civic 30.4 4.93  1    4    2
9  Toyota Corolla 33.9 4.22  1    4    1
10      Fiat X1-9 27.3 4.08  1    4    1
11  Porsche 914-2 26.0 4.43  1    5    2
12   Lotus Europa 30.4 3.77  1    5    2
13   Ferrari Dino 19.7 3.62  1    5    6
14  Maserati Bora 15.0 3.54  1    5    8
15     Volvo 142E 21.4 4.11  1    4    2

Another solution with dplyr :

DF %>%
  select(!!c(1, 28:31))

cf : https://www.rdocumentation.org/packages/dplyr/versions/0.7.6/topics/select

You can also use with the dplyr version 0.8

DF %>%
      select(1, 28:31)
 Select.by.pos <- function(pos, dt){

   return(dt[, pos])

 }

The pos argument should not be a string but a numeeic vector :) dt is a dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!