How to re-label countries by continent and find the mean value

Hi! I'm a little new at R. Essentially, I have a dataframe consisting of countries (32 unique factors), years (from 1960-2019) and TFR. What I'd like to do is group my factor which has countries into the continent that they belong to and find the mean TFR value of those countries within a certain continent in a specific year. I've found out how to re-label the countries into continents with:

tfr_recoded <- tfr %>%
mutate(
Country = fct_collapse(
Country,
"Northern Europe" = c("Estonia","Denmark","Iceland","Finland","Norway","Sweden"),
"Eastern Europe" = c("Bulgaria", "Belarus", "Poland", "Lithuania", "Hungary", "Ukraine"),
"Western Europe" = c("Germany","United Kingdom", "France", "Switzerland", "Austria","Netherlands","Czechia", "Slovenia", "Slovakia", "Croatia"),
"Southern Europe" = c("Italy", "Spain", "Portugal"),
"Asia" = c("Taiwan", "Republic of Korea", "Japan"),
"America" = c("USA", "Chile", "Canada")))

But now I'm struggling to find out how to get the mean TFR of these continents (using the individual TFR rates of the countries) in each year! Added is a screenshot of the dataframe to hopefully help you understand. Many thanks :slight_smile:

You have to use dplyr:group_by()

And dplyr::summarise()

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

It worked great! Thanks so much.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.