Summation by group based on unique values for nested data in r

For the following dataset:

a <- data.table(group_v = rep(c("A","B"),each=4),
                type_v1=c(1,2,2,3,4,4,4,5),
                type_v2=c("1","2a","2b","3","4a","4b","4c","5"),
                number_v=c(12,26,26,35,24,24,24,7))
   group_v type_v1 type_v2 number_v
1:       A      1       1       12
2:       A      2      2a       26
3:       A      2      2b       26
4:       A      3       3       35
5:       B      4      4a       24
6:       B      4      4b       24
7:       B      4      4c       24
8:       B      5       5        7

Is there a straighforward data.table solution that would give the sum of number_v by grouping on type_v1 without considering type_v2.

The problem with this:

a[,sum_num:=sum(number_v),
  .(group_v,type_v2)]
> a
   group_v type_v1 type_v2 number_v sum_num
1:       A      1       1       12      99
2:       A      2      2a       26      99
3:       A      2      2b       26      99
4:       A      3       3       35      99
5:       B      4      4a       24      79
6:       B      4      4b       24      79
7:       B      4      4c       24      79
8:       B      5       5        7      79

is that it counts some rows multiple times. The correct output should be:

   group_v type_v1 type_v2 number_v sum_num
1:       A       1       1       12      73
2:       A       2      2a       26      73
3:       A       2      2b       26      73
4:       A       3       3       35      73
5:       B       4      4a       24      31
6:       B       4      4b       24      31
7:       B       4      4c       24      31
8:       B       5       5        7      31

Thanks in advance.



Read more here: https://stackoverflow.com/questions/67376862/summation-by-group-based-on-unique-values-for-nested-data-in-r

Content Attribution

This content was originally published by lovestacksflow at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: