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

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

Select seems to work with the column indexes (dplyr 0.2), so just match your desired names to their index and use them to select the columns.

myCols <- c("mpg","disp")
colNums <- match(myCols,names(mtcars))
mtcars %>% select(colNums)

You can use get() to get the object named by the string in the current environment. So:

R> iris %>% select(Species, Petal.Length) %>% head(3)
  Species Petal.Length
1  setosa          1.4
2  setosa          1.4
3  setosa          1.3
R> iris %>% select('Species', 'Petal.Length') %>% head(3)
Error in abs(ind[ind < 0]) : 
  non-numeric argument to mathematical function     
R> iris %>% select(get('Species'), get('Petal.Length')) %>% head(3)
  Species Petal.Length
1  setosa          1.4
2  setosa          1.4
3  setosa          1.3
R> s <- 'Species'
R> p <- 'Petal.Length'
R> iris %>% select(get(s), get(p)) %>% head(3)
  Species Petal.Length
1  setosa          1.4
2  setosa          1.4
3  setosa          1.3

Tags:

R