Based on the updated input example, grouped by 'Reg' and the presence of values 10 to 12 in 'Ateco.', get the sum
of 'Numb.' and paste
the 'Ateco', elements, ungroup
and remove the 'grp' if needed
library(tidyverse)
df %>%
group_by(Reg, grp = Ateco. %in% 10:12) %>%
summarise(Numb. = sum(Numb.),
Ateco. = paste(Ateco., collapse="_")) %>%
ungroup %>%
select(-grp)
# A tibble: 3 x 3
# Reg Numb. Ateco.
# <chr> <int> <chr>
#1 A 898 10_11_12
#2 B 414 29
#3 B 898 10_11_12
If we assume the 'grp' is created based on the occurence of 'Ateco' values in both the 'Reg' elements
df %>%
group_by(Ateco.) %>%
group_by(grp = n_distinct(Reg) > 1, Reg) %>%
summarise(Numb. = sum(Numb.),
Ateco. = paste(Ateco., collapse="_")) %>%
ungroup %>%
select(-grp)
Update
Based on the new dataset
df2 %>%
group_by(Ateco. = case_when(Ateco. %in% 10:12 ~ '10_11_12',
TRUE ~ as.character(Ateco.)), Reg) %>%
summarise(Numb. = sum(Numb.))
# A tibble: 6 x 3
# Groups: Ateco. [?]
# Ateco. Reg Numb.
# <chr> <chr> <int>
#1 10_11_12 A 898
#2 10_11_12 B 898
#3 29 B 414
#4 30 B 434
#5 31 B 444
#6 32 B 464
data
df <- structure(list(Ateco. = c(10L, 11L, 12L, 10L, 11L, 12L, 29L),
Numb. = c(223L, 332L, 343L, 223L, 332L, 343L, 414L), Reg = c("A",
"A", "A", "B", "B", "B", "B")), class = "data.frame", row.names = c(NA,
-7L))
df2 <- structure(list(Ateco. = c(10L, 11L, 12L, 10L, 11L, 12L, 29L,
30L, 31L, 32L), Numb. = c(223L, 332L, 343L, 223L, 332L, 343L,
414L, 434L, 444L, 464L), Reg = c("A", "A", "A", "B", "B", "B",
"B", "B", "B", "B")), class = "data.frame", row.names = c(NA,
-10L))