2

我正在尝试使用该tidyselect函数wherepivot_longer收到tidyselect包不支持谓词的错误。这似乎有点不合理,所以很可能我有语法错误。(我意识到 SO 不适用于代码调试。)我认为如果有人能告诉我如何完成这项任务,它会帮助我更好地理解。

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())


这给出了我想要的结果。

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

但我想选择某些类型的字段。

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

证明它确实适用dplyr,所以我没有犯这个错误:(Tidyverse:这个 tidyselect 接口还不支持谓词

d %>% 
  select(where(is.character))
4

1 回答 1

0

使用它tidyr_1.1.0的开发版本dplyr可以正常工作

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
于 2020-05-27T19:05:21.727 回答