0

When I have a variable like:

col_to_select = 2

And I want to use the value from that variable in my dplyr::select, how would I do that?

mtcars %>% 
  select(col_to_select)

gives me obviously this error:

Error in `select()`:
! Can't subset columns that don't exist.
x Column `col_to_select` doesn't exist.

I know that this has to do with tidy eval. But I just can't find the solution now:/

Carl
  • 4,232
  • 2
  • 12
  • 24
Lenn
  • 1,283
  • 7
  • 20
  • 3
    It works here (no tidyeval needed), but it is preferable to use `all_of(col_to_select)`. – harre Jun 13 '22 at 12:24
  • 2
    It also works here. Maybe you should updated your `dplyr`. – PaulS Jun 13 '22 at 12:28
  • 1
    If we run your code as is, in that order, with current dplyr (e.g. >v1.0) it runs fine without that error. So presumably you are either running your code in a different context (perhaps in a function?) or with an older version of dplyr. Can you please explain more? – Jon Spring Jun 13 '22 at 16:09
  • Thanks to all of you!! I think the error must have been on my side by loading the wrong order... – Lenn Jun 13 '22 at 20:56

0 Answers0