challenge_1
railroads
faostat
wildbirds
Reading in data and creating a post
Author

Rahul Somu

Published

February 22, 2023

Code
library(tidyverse)

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

Challenge Overview

Today’s challenge is to

  1. read in a dataset, and

  2. describe the dataset using both words and any supporting information (e.g., tables, etc)

Read in the Data

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

  • railroad_2012_clean_county.csv ⭐
  • birds.csv ⭐⭐
  • FAOstat*.csv ⭐⭐
  • wild_bird_data.xlsx ⭐⭐⭐
  • StateCounty2012.xls ⭐⭐⭐⭐

Find the _data folder, located inside the posts folder. Then you can read in the data, using either one of the readr standard tidy read commands, or a specialized package such as readxl.

Code
df_birds <- read_csv("_data/birds.csv")

Add any comments or documentation as needed. More challenging data sets may require additional code chunks and documentation.

Describe the data

Using a combination of words and results of R commands, can you provide a high level description of the data? Describe as efficiently as possible where/how the data was (likely) gathered, indicate the cases and variables (both the interpretation and any details you deem useful to the reader to fully understand your chosen data).

Code
#summary of data frame
summary(df_birds)
 Domain Code           Domain            Area Code        Area          
 Length:30977       Length:30977       Min.   :   1   Length:30977      
 Class :character   Class :character   1st Qu.:  79   Class :character  
 Mode  :character   Mode  :character   Median : 156   Mode  :character  
                                       Mean   :1202                     
                                       3rd Qu.: 231                     
                                       Max.   :5504                     
                                                                        
  Element Code    Element            Item Code        Item          
 Min.   :5112   Length:30977       Min.   :1057   Length:30977      
 1st Qu.:5112   Class :character   1st Qu.:1057   Class :character  
 Median :5112   Mode  :character   Median :1068   Mode  :character  
 Mean   :5112                      Mean   :1066                     
 3rd Qu.:5112                      3rd Qu.:1072                     
 Max.   :5112                      Max.   :1083                     
                                                                    
   Year Code         Year          Unit               Value         
 Min.   :1961   Min.   :1961   Length:30977       Min.   :       0  
 1st Qu.:1976   1st Qu.:1976   Class :character   1st Qu.:     171  
 Median :1992   Median :1992   Mode  :character   Median :    1800  
 Mean   :1991   Mean   :1991                      Mean   :   99411  
 3rd Qu.:2005   3rd Qu.:2005                      3rd Qu.:   15404  
 Max.   :2018   Max.   :2018                      Max.   :23707134  
                                                  NA's   :1036      
     Flag           Flag Description  
 Length:30977       Length:30977      
 Class :character   Class :character  
 Mode  :character   Mode  :character  
                                      
                                      
                                      
                                      
Code
#get shape and head of data frame
dim(df_birds)
[1] 30977    14
Code
head(df_birds,10)
# A tibble: 10 × 14
   Domain Cod…¹ Domain Area …² Area  Eleme…³ Element Item …⁴ Item  Year …⁵  Year
   <chr>        <chr>    <dbl> <chr>   <dbl> <chr>     <dbl> <chr>   <dbl> <dbl>
 1 QA           Live …       2 Afgh…    5112 Stocks     1057 Chic…    1961  1961
 2 QA           Live …       2 Afgh…    5112 Stocks     1057 Chic…    1962  1962
 3 QA           Live …       2 Afgh…    5112 Stocks     1057 Chic…    1963  1963
 4 QA           Live …       2 Afgh…    5112 Stocks     1057 Chic…    1964  1964
 5 QA           Live …       2 Afgh…    5112 Stocks     1057 Chic…    1965  1965
 6 QA           Live …       2 Afgh…    5112 Stocks     1057 Chic…    1966  1966
 7 QA           Live …       2 Afgh…    5112 Stocks     1057 Chic…    1967  1967
 8 QA           Live …       2 Afgh…    5112 Stocks     1057 Chic…    1968  1968
 9 QA           Live …       2 Afgh…    5112 Stocks     1057 Chic…    1969  1969
10 QA           Live …       2 Afgh…    5112 Stocks     1057 Chic…    1970  1970
# … with 4 more variables: Unit <chr>, Value <dbl>, Flag <chr>,
#   `Flag Description` <chr>, and abbreviated variable names ¹​`Domain Code`,
#   ²​`Area Code`, ³​`Element Code`, ⁴​`Item Code`, ⁵​`Year Code`
Code
# get unique area fields and items
area_unique <- unique(df_birds[ , c("Area Code", "Area")])
area_unique
# A tibble: 248 × 2
   `Area Code` Area               
         <dbl> <chr>              
 1           2 Afghanistan        
 2           3 Albania            
 3           4 Algeria            
 4           5 American Samoa     
 5           7 Angola             
 6           8 Antigua and Barbuda
 7           9 Argentina          
 8           1 Armenia            
 9          22 Aruba              
10          10 Australia          
# … with 238 more rows
Code
items_unique <- unique(df_birds[,c("Item")])

#get rows with empty flags
sum(is.na(select(df_birds, Flag)))
[1] 10773
Code
#get range of year column
year <- select(df_birds, Year)
range_year <- c(min(year),max(year))
range_year
[1] 1961 2018
Code
#Get Unique flags with all variables
flags_unique <- unique(df_birds[ , c("Domain", "Area","Item","Year","Flag","Flag Description")])
flags_unique
# A tibble: 30,977 × 6
   Domain       Area        Item      Year Flag  `Flag Description`
   <chr>        <chr>       <chr>    <dbl> <chr> <chr>             
 1 Live Animals Afghanistan Chickens  1961 F     FAO estimate      
 2 Live Animals Afghanistan Chickens  1962 F     FAO estimate      
 3 Live Animals Afghanistan Chickens  1963 F     FAO estimate      
 4 Live Animals Afghanistan Chickens  1964 F     FAO estimate      
 5 Live Animals Afghanistan Chickens  1965 F     FAO estimate      
 6 Live Animals Afghanistan Chickens  1966 F     FAO estimate      
 7 Live Animals Afghanistan Chickens  1967 F     FAO estimate      
 8 Live Animals Afghanistan Chickens  1968 <NA>  Official data     
 9 Live Animals Afghanistan Chickens  1969 F     FAO estimate      
10 Live Animals Afghanistan Chickens  1970 F     FAO estimate      
# … with 30,967 more rows