2

I am trying to use the tidyselect function where with pivot_longer and am getting the error that the tidyselect package doesn't support predicates. That was seem somewhat unreasonable, so most likely I have a syntax error. (I realize that SO is not for code debugging.) I think it would help me understand better if someone could show me how to accomplish this task.

d <- dplyr::tribble(
  ~cups, ~glasses,
  "YES", "NO",
  "NO" , "YES",
  "YES", "NO",
  "YES", "NO",
  "NO" , "YES",
  "YES", "NO",
  "NO" , "YES",
  "NO" , "YES",
  "YES", "NO",
  "NO" , "YES",
  "YES", "NO",
  "NO" , "YES",
  "abc", "def"
) %>% 
  mutate(id = row_number())


This gives the result I want.

the_columns_I_want <- c('cups','glasses')
d %>% 
  pivot_longer(all_of(the_columns_I_want),values_to = 'result', names_to =  'group')    

But I want to select certain types of fields.

d %>% 
  pivot_longer(where(is.character),values_to = 'result', names_to =  'group')    

Show that it does work for dplyr, So I am not making this mistake: (Tidyverse: This tidyselect interface doesn't support predicates yet)

d %>% 
  select(where(is.character))
Harlan Nelson
  • 1,394
  • 1
  • 10
  • 22

1 Answers1

0

With the tidyr_1.1.0 and the devel version of dplyr it is working fine

library(tidyr)
d %>% 
  pivot_longer(where(is.character),values_to = 'result', names_to =  'group')    
# A tibble: 26 x 3
#      id group   result
#   <int> <chr>   <chr> 
# 1     1 cups    YES   
# 2     1 glasses NO    
# 3     2 cups    NO    
# 4     2 glasses YES   
# 5     3 cups    YES   
# 6     3 glasses NO    
# 7     4 cups    YES   
# 8     4 glasses NO    
# 9     5 cups    NO    
#10     5 glasses YES   
# … with 16 more rows
akrun
  • 874,273
  • 37
  • 540
  • 662
  • You still have `everything()` in the pivot_longer. Note that my `select(where(is.character))` does the same thing as your `select_if`. You are saying that I need two steps and can't do the selection in pivot_longer. Also, you don't keep the `id` field. – Harlan Nelson May 27 '20 at 19:25
  • @HarlanNelson not clear from your post because both the methods you showed is working fine for me – akrun May 27 '20 at 19:27
  • @HarlanNelson What is your package version of tidyr. It is working fine for me with `where` – akrun May 27 '20 at 19:30
  • tidyr 1.0.2 does not work for me: d %>% pivot_longer(where(is.character),values_to = 'result', names_to = 'group') – Harlan Nelson May 27 '20 at 19:31
  • @HarlanNelson that code is working for me. Please check your package version of tidyr and dplyr – akrun May 27 '20 at 19:31
  • 1
    I need tidyr_1.1.0. Thanks – Harlan Nelson May 27 '20 at 19:37