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)
# 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
---title: "Challenge 8"author: "Connor Landreth"description: "Joining Data"date: "2023-04-30"format: html: toc: true code-copy: true code-tools: truecategories: - challenge_8 - bird remake - faostat---```{r}#| label: setup#| warning: false#| message: falselibrary(knitr)library(tidyverse)library(ggplot2)library(readxl)library(lubridate)library(here)library(ggthemes)knitr::opts_chunk$set(echo =TRUE, warning=FALSE, message=FALSE)getwd()setwd("C:/Github Projects/601_Spring_2023/posts/_data")```## Read in dataRead in one (or more) of the following datasets, using the correct R package and command. - FAO - bird```{r}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 ```{r}fao <- fao %>%select(Country.Group, Country)%>%rename(country_group ="Country.Group")%>%distinct()fao %>%filter(country_group =="Southern Europe")```Knit table with country groups and associated country groups```{r}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)``````{r}fao%>%summarise(n=n())/277```Nested sub regions within regions:```{r}fao%>%filter(str_detect(country_group, "[aA]nnex"))%>%group_by(country_group)%>%summarise(n=n())``````{r}fao%>%filter(str_detect(country_group, "[aA]nnex"))%>%summarise(n=n())```## Join Data - Combine data sets (marriage & debt)```{r}fao%>%filter(str_detect(country_group, "[iI]ncome"))%>%group_by(country_group)%>%summarise(n=n())``````{r}fao%>%filter(str_detect(country_group, "[iI]ncome"))%>%summarise(n=n())``````{r}fao%>%filter(str_detect(country_group, "[Dd]evelop|OECD"))%>%group_by(country_group)%>%summarise(n=n())``````{r}fao%>%filter(str_detect(country_group, "[Dd]evelop|OECD"))%>%summarise(n=n())``````{r}major_regions<-c("Africa", "Asia", "Europe", "Americas", "Oceania", "Antarctic Region")fao%>%filter(country_group %in% major_regions)%>%summarise(n=n())```Create new categorical variables for the country groups for data set.```{r}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"))``````{r}nrow(birds)``````{r}birds <-left_join(birds, fao_regions_wide,by =c("Area"="Country"))```Then we can summarize the data across the countries ```{r}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")```