Summing values within group

Hi,

I am trying to write a function that does simple aggregation by group.

Suppose I have two variables, groupid and values:

groupid values
1 3
1 2
1 4
2 2
2 2
2 3

The output I want is 2x1 vector,

9
7

Is there any easy way to go about this?

Thank you!

Are you trying to do this in Stan? It’s easy to work on rows or columns by slicing in Stan, but I’m not sure what 9 and 7 are supposed to represent. In general, if you have two variables:

array[N] int x;
array[N] int y;

vector[2] = [foo(x), bar(y)]';

Where foo and bar are the functions to apply to x and y.