0

Here is my data:

df <- tibble::tribble(
   ~A,  ~B, ~C,
  "a", "b", 2L,
  "a", "b", 4L,
  "c", "d", 3L,
  "c", "d", 5L
  )

var <- "AB"

I want to get this output:

df1 <- df %>% 
  unite("AB", c("A", "B")) %>% 
  group_by(AB) %>% 
  nest()

However, I want to refer var, maybe using rlang. I do not want to manually input "AB". I tried the following, but not getting the desired output.

df1 <- df %>% 
  unite(var, c("A", "B")) %>% 
  group_by(!!var) %>% 
  nest()
halfer
  • 19,824
  • 17
  • 99
  • 186
Geet
  • 2,515
  • 2
  • 19
  • 42

1 Answers1

0

This solved the problem:

df1 <- df %>% 
  unite(!!var, c("A", "B")) %>% 
  group_by(!!sym(var)) %>% 
  nest()
Geet
  • 2,515
  • 2
  • 19
  • 42