0

I have read a few stackoverflow questions on NSE with dplyr (here, here and here), but I am still struggling to apply it to my use.

This is my example

This work

yo <- tibble(ah = 1:10, meh = 11:20)
yup <- yo %>% select(ABX = meh)

But I would like to make this work:

i=1
newnames <- c("ABX", "TDX")
yup <- yo %>% select(newnames[i] = meh)

A few things, I have tried without success:

yup <- yo %>% select_(newnames[i] = "meh")
yup <- yo %>% select(!!rlang::sym(newnames[i]) = meh)
yup <- yo %>% select(as.name(newnames[i]) = meh)

Any help? Thanks in advance.

Franky
  • 721
  • 3
  • 11
  • 19
  • 1
    The second attempt but with `:=` instead of `=`? – joran Nov 01 '18 at 21:58
  • Thanks @joran, I don't remember ever seen that use of := .... and I have use R for a few years now. Mmmhhh still so much to learn! – Franky Nov 01 '18 at 22:03

1 Answers1

2
yup <- yo %>% select(!!newnames[i] := meh)

> yup
# A tibble: 10 x 1
     ABX
   <int>
 1    11
 2    12
 3    13
 4    14
 5    15
 6    16
 7    17
 8    18
 9    19
10    20

Not sure if you've read the programming with dplyr vignette, but I've found it very helpful in bettering my understanding of NSE in the tidyverse.

zack
  • 5,205
  • 1
  • 19
  • 25
  • Oh my ... nope I did not read the vignette. Homework for the weekend ;-) I have just skim through it and I have a feeling that a whole new R world is opening to me. – Franky Nov 01 '18 at 22:05