challenge_1
Reading in data and creating a post
Author

Daniel Manning

Published

December 28, 2022

Code
library(tidyverse)
library(here)
library(readxl)

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.xlsx ⭐⭐⭐⭐

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.

I changed my working directory to the location of the csv file within the local folder on my desktop. I then used read.csv() to store the railroad dataset within my railroad variable. Based on the output when calling the variable railroads, the state column and county columns consist of characters while the total_employee column consists of integers.

Code
railroad <- here("posts","_data","railroad_2012_clean_county.csv")%>%
  read_csv()
railroad
# A tibble: 2,930 × 3
   state county               total_employees
   <chr> <chr>                          <dbl>
 1 AE    APO                                2
 2 AK    ANCHORAGE                          7
 3 AK    FAIRBANKS NORTH STAR               2
 4 AK    JUNEAU                             3
 5 AK    MATANUSKA-SUSITNA                  2
 6 AK    SITKA                              1
 7 AK    SKAGWAY MUNICIPALITY              88
 8 AL    AUTAUGA                          102
 9 AL    BALDWIN                          143
10 AL    BARBOUR                            1
# … with 2,920 more rows

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).

First I used the head() function to determine that the dataset has three variables, including state (with a two letter code), county (with a three letter code), and total_employees (representing the total railroad employees per county). This dataset was likely gathered by survey. Then I used dim() to show that their are 2930 rows representing the total number of counties with railroad employees. Next I stored the columns into individual variables for ease of use later on. I used the n_distinct() and distinct() functions to show that there were 53 unique entries in the state column and to ouput these values. I then calculated summary statistics for the total employees. Next I used the with() and t_apply() function to output the number of railroad employees in each state and store them in a new variable. Lastly, I calculated summary statistics of this new by_states variable.

Code
head(railroad)
# A tibble: 6 × 3
  state county               total_employees
  <chr> <chr>                          <dbl>
1 AE    APO                                2
2 AK    ANCHORAGE                          7
3 AK    FAIRBANKS NORTH STAR               2
4 AK    JUNEAU                             3
5 AK    MATANUSKA-SUSITNA                  2
6 AK    SITKA                              1
Code
dim(railroad)
[1] 2930    3
Code
state <- railroad$state
county <- railroad$county
total_employees <- railroad$total_employees

railroad %>% 
  select(state) %>%
  n_distinct(.)
[1] 53
Code
railroad %>% 
  select(state) %>%
  distinct()
# A tibble: 53 × 1
   state
   <chr>
 1 AE   
 2 AK   
 3 AL   
 4 AP   
 5 AR   
 6 AZ   
 7 CA   
 8 CO   
 9 CT   
10 DC   
# … with 43 more rows
Code
mean(total_employees)
[1] 87.17816
Code
median(total_employees)
[1] 21
Code
min(total_employees)
[1] 1
Code
max(total_employees)
[1] 8207
Code
sd(total_employees)
[1] 283.6359
Code
var(total_employees)
[1] 80449.32
Code
IQR(total_employees)
[1] 58
Code
by_state <- railroad %>% with(tapply(total_employees,state,FUN=sum))

mean(by_state)
[1] 4819.472
Code
median(by_state)
[1] 3379
Code
min(by_state)
[1] 1
Code
max(by_state)
[1] 19839
Code
sd(by_state)
[1] 4781.829
Code
var(by_state)
[1] 22865885
Code
IQR(by_state)
[1] 4175