dplyr: select columns by position in NSE

Jordi Vidal picture Jordi Vidal · Feb 27, 2017 · Viewed 33.1k times · Source

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?

Answer

MrFlick picture MrFlick · Feb 27, 2017

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

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