Silvia
Silvia

Reputation: 405

Sum values according to specific condition in R

i have a df structured like this:

 Ateco. Numb. Reg

   10    223   A
   11    332   A
   12    343   A
   10    223   B
   11    332   B
   12    343   B
   29    414   B
   30    434   B
   31    444   B
   32    464   B

and I want to obtain another df, where numb is the sum of the Ateco values that I select.

 Ateco.     Numb.  Reg

10_11_12    898     A
10_11_12    898     B      
   29       414     B
   30       434     B
   31       444     B
   32       464     B

how could I do?

Upvotes: 2

Views: 44

Answers (1)

akrun
akrun

Reputation: 886938

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))

Upvotes: 2

Related Questions