Challenge 8

challenge_8
bird remake
faostat
Joining Data
Author

Connor Landreth

Published

April 30, 2023

library(knitr)
library(tidyverse)
library(ggplot2)
library(readxl)
library(lubridate)
library(here)
library(ggthemes)

knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)

getwd()
[1] "C:/Github Projects/601_Spring_2023/posts"
setwd("C:/Github Projects/601_Spring_2023/posts/_data")

Read in data

Read in one (or more) of the following datasets, using the correct R package and command.

  • FAO
  • bird
library(readxl)
setwd("C:/Github Projects/601_Spring_2023/posts/_data")
#milmar <- read_excel("ActiveDuty_MaritalStatus.xls")
#Amar <- read.csv("australian_marriage_tidy.csv")
birds <- read.csv("birds.csv")
fao <- read.csv("FAOSTAT_country_groups.csv")

Going to work through the fao and bird joining example. First, rename variables

fao <- fao %>% 
  select(Country.Group, Country)%>%
  rename(country_group = "Country.Group")%>%
  distinct()

fao %>% 
  filter(country_group == "Southern Europe")
     country_group                      Country
1  Southern Europe                      Albania
2  Southern Europe                      Andorra
3  Southern Europe       Bosnia and Herzegovina
4  Southern Europe                      Croatia
5  Southern Europe                    Gibraltar
6  Southern Europe                       Greece
7  Southern Europe                     Holy See
8  Southern Europe                        Italy
9  Southern Europe                       Kosovo
10 Southern Europe                        Malta
11 Southern Europe                   Montenegro
12 Southern Europe              North Macedonia
13 Southern Europe                     Portugal
14 Southern Europe                   San Marino
15 Southern Europe                       Serbia
16 Southern Europe        Serbia and Montenegro
17 Southern Europe                     Slovenia
18 Southern Europe Southern Europe, unspecified
19 Southern Europe                        Spain
20 Southern Europe                 Yugoslav SFR

Knit table with country groups and associated country groups

temp<-fao%>%
  group_by(country_group)%>%
  summarize(n=n())%>%
  arrange(desc(n))
half <-c(1:round(nrow(temp)/2))
knitr::kable(list(temp[half,],  
           matrix(numeric(), nrow=0, ncol=1),
           temp[-half,]), 
           caption = "# Coutnries by Country Groups")%>%
  kableExtra::kable_styling(font_size=15)
# Coutnries by Country Groups
country_group n
World 277
Non-Annex I countries 161
Net Food Importing Developing Countries 81
Annex I countries 78
High-income economies 64
Africa 63
Europe 63
Americas 61
Small Island Developing States 58
Upper-middle-income economies 56
Low Income Food Deficit Countries 55
Asia 54
Sub-Saharan Africa 53
Latin America and the Caribbean 52
Least Developed Countries 51
Sub-Saharan Africa (including Sudan) 49
Lower-middle-income economies 46
Northern America and Europe 44
OECD 36
Oceania 35
Low income economies 34
Land Locked Developing Countries 32
Caribbean 30
European Union (27) 30
Western Asia and Northern Africa 24
Eastern Africa 23
Eastern Asia and South-eastern Asia 20
Southern Europe 20
Western Asia 20
country_group n
Northern Europe 18
Oceania excluding Australia and New Zealand 18
Western Africa 18
South America 17
Central Asia and Southern Asia 14
North and Central America 14
Western Asia (exc. Armenia, Azerbaijan, Cyprus, Israel and Georgia) 13
Eastern Europe 12
Polynesia 12
South-eastern Asia 11
Western Europe 11
Micronesia 10
Eastern Asia 9
Middle Africa 9
Southern Asia 9
Caucasus and Central Asia 8
Central America 8
Northern Africa 8
Southern Asia (excluding India) 8
Australia and New Zealand 6
Eastern Asia (excluding Japan) 6
Northern Africa (excluding Sudan) 6
Northern America 6
Central Asia 5
Melanesia 5
Southern Africa 5
Eastern Asia (excluding China) 3
Eastern Asia (excluding Japan and China) 3
Antarctic Region 1
fao%>%
  summarise(n=n())/277
        n
1 7.01444

Nested sub regions within regions:

fao%>%
  filter(str_detect(country_group, "[aA]nnex"))%>%
  group_by(country_group)%>%
  summarise(n=n())
# A tibble: 2 × 2
  country_group             n
  <chr>                 <int>
1 Annex I countries        78
2 Non-Annex I countries   161
fao%>%
  filter(str_detect(country_group, "[aA]nnex"))%>%
  summarise(n=n())
    n
1 239

Join Data - Combine data sets (marriage & debt)

fao%>%
  filter(str_detect(country_group, "[iI]ncome"))%>%
  group_by(country_group)%>%
  summarise(n=n())
# A tibble: 5 × 2
  country_group                         n
  <chr>                             <int>
1 High-income economies                64
2 Low income economies                 34
3 Low Income Food Deficit Countries    55
4 Lower-middle-income economies        46
5 Upper-middle-income economies        56
fao%>%
  filter(str_detect(country_group, "[iI]ncome"))%>%
  summarise(n=n())
    n
1 255
fao%>%
  filter(str_detect(country_group, "[Dd]evelop|OECD"))%>%
  group_by(country_group)%>%
  summarise(n=n())
# A tibble: 5 × 2
  country_group                               n
  <chr>                                   <int>
1 Land Locked Developing Countries           32
2 Least Developed Countries                  51
3 Net Food Importing Developing Countries    81
4 OECD                                       36
5 Small Island Developing States             58
fao%>%
  filter(str_detect(country_group, "[Dd]evelop|OECD"))%>%
  summarise(n=n())
    n
1 258
major_regions<-c("Africa", "Asia", "Europe", "Americas", 
                 "Oceania", "Antarctic Region")

fao%>%
  filter(country_group %in% major_regions)%>%
  summarise(n=n())
    n
1 277

Create new categorical variables for the country groups for data set.

fao_regions_wide<-fao%>%
  filter(country_group!="World")%>%
  pivot_wider(names_from=country_group, values_from = 1)%>%
  unite("gp_annex", contains("Annex"), 
        sep="", na.rm=TRUE, remove=TRUE)%>%
  unite("gp_major_region", any_of(major_regions), 
        sep="", na.rm=TRUE, remove=TRUE)%>%
  unite("gp_income", contains("Income")|contains("income"),
        sep="", na.rm=TRUE, remove=TRUE)%>%
  unite("gp_develop", contains("Develop")|contains("OECD"),
        sep="", na.rm=TRUE, remove=TRUE)%>%
  select(Country, starts_with("gp"))
nrow(birds)
[1] 30977
birds <- left_join(birds, fao_regions_wide,
                   by = c("Area" = "Country"))

Then we can summarize the data across the countries

eggs_chicken <- here("posts","_data","FAOSTAT_egg_chicken.csv") %>%
  read_csv()
cattle <- here("posts","_data","FAOSTAT_egg_chicken.csv") %>%
  read_csv()
data <- left_join(eggs_chicken, cattle, by="Area")