Names' Attribute Must Be the Same Length as the Vector

dplyr case_when throws error names' attribute [1] must be the same length as the vector [0]

I believe you need to correct TRUE ~ open to TRUE ~ open_flag:

ERROR:

d %>% 
mutate(
open_flag = case_when(
open_flag == 0 & (click_flag > 0 | mirror_flag > 0) ~ 1,
TRUE ~ open
)
)

Error in `mutate()`:
! Problem while computing `open_flag = case_when(...)`.
Caused by error in `` names(message) <- `*vtmp*` ``:
! 'names' attribute [1] must be the same length as the vector [0]
Run `rlang::last_error()` to see where the error occurred.

CORRECT:

d %>% 
mutate(
open_flag = case_when(
open_flag == 0 & (click_flag > 0 | mirror_flag > 0) ~ 1,
TRUE ~ open_flag
)
)

open_flag click_flag mirror_flag
1 0 -1 0
2 2 0 0
3 1 1 3

Input:

d <- data.frame(
open_flag = c(0, 2, 0),
click_flag = c(-1, 0, 1),
mirror_flag = c(0, 0, 3)
)

names' attribute [4000] must be the same length as the vector [5]

setNames is what you need:

setNames(l, unique(df$ID))

Output:

$A
[1] 1

$B
[1] 2

$C
[1] 3

$D
[1] 4

$E
[1] 5


Related Topics



Leave a reply



Submit