Load required libraries
library(conflicted)
library(purrr)
library(dplyr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ readr 2.1.4
conflicts_prefer(dplyr::filter)
## [conflicted] Will prefer dplyr::filter over any other package.
Read data
cattle_dairy <- read.csv("challenge_datasets/FAOSTAT_cattle_dairy.csv")
egg_chicken <- read.csv("challenge_datasets/FAOSTAT_egg_chicken.csv")
livestock <- read.csv("challenge_datasets/FAOSTAT_livestock.csv")
purrr
analyze_trends <- function(country, cattle_dairy, egg_chicken, livestock) {
# Extract data for the given country from each dataset
dairy_data <- filter(cattle_dairy, Area == country)
egg_data <- filter(egg_chicken, Area == country)
livestock_data <- filter(livestock, Area == country)
# Create a summary of trends
summary <- list(
Country = country,
DairyTrend = summary(dairy_data$Value),
EggTrend = summary(egg_data$Value),
LivestockTrend = summary(livestock_data$Value)
)
return(summary)
}
# List of top countries
top_countries <- c("Afghanistan", "China", "India", "Germany", "Japan")
# Apply the function to each country using map
results <- map(top_countries, analyze_trends, cattle_dairy, egg_chicken, livestock)
results
## [[1]]
## [[1]]$Country
## [1] "Afghanistan"
##
## [[1]]$DairyTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 3592 5459 600000 903662 1361100 4050000
##
## [[1]]$EggTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4000 8800 14325 15203 21225 29048
##
## [[1]]$LivestockTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 20000 200000 1300000 3597216 3787500 21500000
##
##
## [[2]]
## [[2]]$Country
## [1] "China"
##
## [[2]]$DairyTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
##
##
## [[2]]$EggTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
##
##
## [[2]]$LivestockTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
##
##
##
## [[3]]
## [[3]]$Country
## [1] "India"
##
## [[3]]$DairyTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 3298 10491 18322000 19678511 32565440 89833590
##
## [[3]]$EggTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 35000 83542 119868 608512 447500 5236935
##
## [[3]]$LivestockTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 52813 825500 10246848 48618161 76858006 204584000
##
##
## [[4]]
## [[4]]$Country
## [1] "Germany"
##
## [[4]]$DairyTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 30992 66294 6635978 11923510 28064424 34538496
##
## [[4]]$EggTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 33400 83929 151722 387334 810025 1213480
##
## [[4]]$LivestockTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 4200 440546 2740200 9617191 19666752 37342448 49
##
##
## [[5]]
## [[5]]$Country
## [1] "Japan"
##
## [[5]]$DairyTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 37513 70972 1219500 2638715 4955461 8657000
##
## [[5]]$EggTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 55922 139260 173922 816778 1814000 2627764
##
## [[5]]$LivestockTrend
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 9000 22625 133848 2658424 4529250 11866000