Is there a typo ? as it seems guaranteed that any positive number would be smaller than itself plus an positive integer, so not a useful criteria to to split on...
That said heres a basic example of doing the 'kind of thing'.
library(tidyverse)
set.seed(42)
(exdf<- tibble(
creation_date = sample(seq.Date(from=as.Date("2019/01/01"),by="day",length.out = 400),
size = 1000,
replace=TRUE
),
age = sample.int(3,size=1000,replace = TRUE)*20+15,
location = sample(letters[1:5],
size = 1000,
replace=TRUE),
device= sample(LETTERS,
size = 1000,
replace=TRUE)
))
(group_df <- group_by(exdf,
age,
location,
device) %>% summarise(
avg_date =mean(creation_date)
) %>% ungroup %>% arrange(age,location,device) %>%
mutate(group_num= row_number()))
(df2 <-left_join(group_df,
exdf) %>% mutate(greater_than_average = creation_date > avg_date,
final_group_code = paste0(group_num,str_sub(greater_than_average,start = 1,end=1))))