Error when using dplyr::between() within case_when()

I have following data set:

full_name day_since_last_order order_count

1 123 qwe 665 1
2 A AZIS BAJURI 399 1
3 A Huzaime Bin Abdul Hamid 327 1
4 a rahman mohamad 709 1
5 A Ruthirannessan AL K Ahsogan 58 2

Summary of the data set:

full_name day_since_last_order order_count
Length:16291 Min. : 1.0 Min. : 1.000
Class :character 1st Qu.: 307.0 1st Qu.: 1.000
Mode :character Median : 537.0 Median : 1.000
Mean : 520.4 Mean : 1.165
3rd Qu.: 760.0 3rd Qu.: 1.000
Max. :1133.0 Max. :11.000

I try to group "order_count" with codes below with case_when():

customer_seg %>% mutate(order_count = case_when(order_count %in% c(1, 2, 3) ~ order_count,
between(order_count, 4, 5) ~ "4-5",
between(order_count, 6, 9) ~ "6-9",
order_count > 9 ~ "10+")

I got an error message:
Error: Problem with mutate() input order_count.
x must be an integer vector, not a character vector.
:information_source: Input order_count is case_when(...).

Please advise which part goes wrong.

all outcomes for case_when must be explicitly the same type so try

 case_when(order_count %in% c(1, 2, 3) ~ as.character(order_count),
between(order_count, 4, 5) ~ "4-5",
between(order_count, 6, 9) ~ "6-9",
order_count > 9 ~ "10+")
1 Like

I learn something new today. TQVM.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.