For the following simple dataset;
row country year
1 NLD 2005
2 NLD 2005
3 BLG 2006
4 BLG 2005
5 GER 2005
6 NLD 2007
7 NLD 2005
8 NLD 2008
the following code:
df[, .N, by = list(country, year)][,prop := N/sum(N)]
gives the proportion of observations compared to the total of observations. What I want however is to measure the proportion for each country. How should I adapt this code to give me the correct proportions?
Desired output:
row country year prop
1 NLD 2005 0.6
2 NLD 2005 0.6
3 BLG 2006 0.5
4 BLG 2005 0.5
5 GER 2005 1
6 NLD 2007 0.2
7 NLD 2005 0.6
8 NLD 2008 0.2