I have a dataset with the number of suicides and population broken down by age group for each year and country, including NA values for some countries/years.
I would like to sum the number of suicides and populations across age groups for each year and country but leave NA if a given country has no data for a given year.
Input would look like:
country = c("Albania", "Albania", "Croatia", "Croatia", "Croatia", "Croatia")
year = c("1991", "1991", "1991", "1991", "1992", "1992"
suicides_no = c(NA, NA, 5, 3, 12, 9)
population = c(100, 200, 50, 75, 250, 300)
df = data.frame(country, year, suicides_no, population
and the output columns would be:
country year suicides_no population
Albania 1991 NA 300
Croatia 1991 8 125
Croatia 1992 21 550