Combining unite and select_if

Suppose I have something like this:

library(tidyr)
d=tribble(
  ~x1, ~x2, ~y,
 "a", "one", 7,
 "b", "one", 9,
 "a", "two", 11
)
d

and I want to combine x1 and x2 to create a combo column, like this:

d %>% unite(combo,x1,x2)

Is there a way to take advantage of the fact that x1 and x2 are the only two character columns? I want to be able to say something like (this gives an error):

d %>% unite(combo, select_if(is.character))

Is there a way to do this that will work?

Hi Ken, it looks to me like your code is nearly there. I think it just needs a slight tweak:

library(dplyr)
library(tidyr)
d <- tribble(
  ~x1, ~x2, ~y,
  "a", "one", 7,
  "b", "one", 9,
  "a", "two", 11
)

d %>% { unite(., combo, names(select_if(., is.character))) }
#> # A tibble: 3 x 2
#>   combo     y
#>   <chr> <dbl>
#> 1 a_one     7
#> 2 b_one     9
#> 3 a_two    11
3 Likes

thank you! It looks as if the "names" part is what I was missing.