Disclaimer - title might be misleading - I think part of the reason why I haven't found a solution is that I don't exactly know what to google.
I have a group-level data set in an expanded format; years and country code repeated for every group (id) such as below (manual input)
year country id v1 v2 v3
1991 20 1 1 0 0
1991 20 2 0 1 0
1991 20 3 0 0 1
1991 20 4 1 0 0
1991 20 5 1 0 0
1991 20 6 0 1 0
I want to add country-year counts as columns at the end so it would look like the following
year country id v1 v2 v3 v1.count v2.count v3.count
1991 20 1 1 0 0 3 2 1
1991 20 2 0 1 0 3 2 1
1991 20 3 0 0 1 3 2 1
1991 20 4 1 0 0 3 2 1
1991 20 5 1 0 0 3 2 1
1991 20 6 0 1 0 3 2 1
I have tried aggregate
, count
, and dplyr
with no success. I thought Group by and conditionally count or Frequency count for a specific category might do the trick but I could not get it to work. How can I accomplish this?