dplyr: select columns by position in NSE

39,817

Solution 1

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

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

Solution 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)

Solution 3

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
Share:
39,817
Jordi Vidal
Author by

Jordi Vidal

Updated on February 28, 2020

Comments

  • Jordi Vidal
    Jordi Vidal about 4 years

    I am trying to create a function that will select columns in a DF based on their position. I will always need the first column and then a subset of the DF. I have 1 object for each subset I need to select.

    So far I have tried the following:

    position <- "1,28:31"
    DF %>%
      select_(.dots = position)
    

    but I receive the following error:

    Error in parse(text = x) : :1:2: unexpected 'x'

    It would seem the problem is the comma separation in the column position.

    Is there a workaround?