Hi everyone.
I have a sample dataset below. There are 10 age groups.

age_groups

code

n

5-10

1

10

5-10

2

20

5-10

3

30

5-10

4

40

11-16

1

20

11-16

2

40

11-16

3

10

11-16

4

20

I want to produce the table below where the ratio column (in 2dp) follows the formula [(sum of codes 3 and 4)/(sum of codes 2 and 4)] for each age group.

is the model for the next 8 age groups d, e, f, g, h, i, j, k (skip c because it's the name of the built-in function).

similarly is extended as

result[3,2] <- find_ratio(d)

etc.

This all looks more complicated than it really isâ€”using the [ subset operator, though, is both powerful and easy to apply once understood. Together with which it vastly decreases the amount of syntax required for this class of problems, compared to alternatives.