为什么 case_when 不能返回不同长度的向量?

Dav*_*d T 5 r dplyr

这失败了:

library(tidyverse)
myFn <- function(nmbr){
  case_when(
    nmbr > 3 ~ letters[1:3],
    TRUE ~ letters[1:2]
  )
}

myFn(4)
# Error: `TRUE ~ letters[1:2]` must be length 3 or one, not 2
# Run `rlang::last_error()` to see where the error occurred.
Run Code Online (Sandbox Code Playgroud)

为什么会失败?为什么case_when其分支不能返回不同长度的向量?我想myFn工作,以便我可以做以下事情:

tibble(fruit = c("apple", "grape"),
       count = 3:4) %>% 
  mutate(bowl = myFn(count)) %>% 
  unnest(col = "bowl")
Run Code Online (Sandbox Code Playgroud)

并得到

# A tibble: 5 x 3
fruit count  bowl
<chr> <int> <int>
1 apple     3     a
2 apple     3     b
3 grape     4     a
4 grape     4     b
5 grape     4     c
Run Code Online (Sandbox Code Playgroud)

myFn我可以让它工作 - 通过使用编写非向量化if/else,然后将其包装在 中map,但为什么我必须这样做?

r2e*_*ans 5

根据我的评论,您的函数需要为每一行输入返回一个元素。然而,每个元素的list长度可以为 0 或更大(以及任意复杂度)。尝试这个:

myFn <- function(nmbr){
  case_when(
    nmbr > 3 ~ list(letters[1:3]),
    TRUE ~ list(letters[1:2])
  )
}
tibble(fruit = c("apple", "grape"),
       count = 3:4) %>%
  mutate(bowl = myFn(count))
# # A tibble: 2 x 3
#   fruit count bowl     
#   <chr> <int> <list>   
# 1 apple     3 <chr [2]>
# 2 grape     4 <chr [3]>
tibble(fruit = c("apple", "grape"),
       count = 3:4) %>%
  mutate(bowl = myFn(count)) %>%
  unnest(col = "bowl")
# # A tibble: 5 x 3
#   fruit count bowl 
#   <chr> <int> <chr>
# 1 apple     3 a    
# 2 apple     3 b    
# 3 grape     4 a    
# 4 grape     4 b    
# 5 grape     4 c    
Run Code Online (Sandbox Code Playgroud)