I am trying to extract a data frame of summarised values from a larger data frame as given below:
Year = c(2000,2001,2002,2003,2000,2001,2002,2003,2000,2001,2002,2003,2000,2001,2002,2003)
Country_Name = rep(c("Afghanistan", "Brazil", "Germany", "Italy"), each=4)
total_population = c(10,15,13,12,11,16,14,13,12,17,15,14,13,18,16,15)
Life_expectancy = c(60,67,70,73,61,68,71,74,62,69,72,75,2,69,72,75)
Hospitals = rep(c(5,10,15,13), each=4)
GDP = rep(c(5,7,10,8), each=4)
Example <- data.frame(Year=Year, Country=Country_Name, Population=total_population, Life=Life_expectancy, Hospitals=Hospitals, GDP=GDP)
Example
What I would like to do is summarise this data frame, and extract mean population, mean GDP etc etc by country. I was able to code this to get output for individual countries as such:
country_func <- function(x, df){
#take summary statistics of 4 variables from tidy dataset
#x denotes the country name
#ave population
ave_population <- mean(df$`Population`[df$`Country`==x], na.rm=TRUE)
#median life
median_life <- median(df$`Life`[df$`Country`==x], na.rm=TRUE)
#ave hospital beds per 1000
ave_hospital_beds <- mean(df$`Hospitals`[df$`Country`==x], na.rm=TRUE)
#Health expenditure per capita
mean_GDP <- mean(df$`GDP`[df$`Country`==x], na.rm=TRUE )
data.frame(ave_population=ave_population, median_life=median_life, ave_hospital_beds=ave_hospital_beds, mean_GDP=mean_GDP)
}
country_func('Afghanistan', Example)
But what I would like to do, is for the function to return a data frame that gives those summary statistics for each of the four countries in one go, so one data frame of 4 rows (one for each country), rather than doing it individually each time.
Thanks in advance.