challenge_2
StateCounty
Data wrangling: using group() and summarise(
Author

Lindsay Jones

Published

August 16, 2022

Code
library(tidyverse)

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

Challenge Overview

Today’s challenge is to

  1. read in a data set, and describe the data using both words and any supporting information (e.g., tables, etc)
  2. provide summary statistics for different interesting groups within the data, and interpret those statistics

Read in the Data

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

  • railroad*.csv or StateCounty2012.xlsx ⭐
  • hotel_bookings ⭐⭐⭐
  • FAOstat*.csv ⭐⭐⭐⭐⭐ (join FAOSTAT_country_groups)
Code
library(readxl)
Railroad <- read_xls("_data/StateCounty2012.xls", 
    skip =3)
View(Railroad)

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

Total railroad employment by state and county for calendar year 2012.

Code
library(readxl)
Railroad <- read_xls("_data/StateCounty2012.xls", 
    skip =3)
View(Railroad)

Provide Grouped Summary Statistics

Conduct some exploratory data analysis, using dplyr commands such as group_by(), select(), filter(), and summarise(). Find the central tendency (mean, median, mode) and dispersion (standard deviation, mix/max/quantile) for different subgroups within the data set.

The total number of railroad employees for each state/territory are shown below.

Code
state_totals = Railroad %>% 
  filter(grepl("Total", STATE)) %>%
  filter(!grepl("Grand", STATE))
print(state_totals)
# A tibble: 53 × 5
   STATE     ...2  COUNTY ...4  TOTAL
   <chr>     <lgl> <chr>  <lgl> <dbl>
 1 AE Total1 NA    <NA>   NA        2
 2 AK Total  NA    <NA>   NA      103
 3 AL Total  NA    <NA>   NA     4257
 4 AP Total1 NA    <NA>   NA        1
 5 AR Total  NA    <NA>   NA     3871
 6 AZ Total  NA    <NA>   NA     3153
 7 CA Total  NA    <NA>   NA    13137
 8 CO Total  NA    <NA>   NA     3650
 9 CT Total  NA    <NA>   NA     2592
10 DC Total  NA    <NA>   NA      279
# … with 43 more rows
# ℹ Use `print(n = ...)` to see more rows

Here is the median number of railroad employees per state:

Code
summarize(state_totals,median(TOTAL))
# A tibble: 1 × 1
  `median(TOTAL)`
            <dbl>
1            3379

The mean number railroad employees in each state is:

Code
summarize(state_totals,mean(TOTAL))
# A tibble: 1 × 1
  `mean(TOTAL)`
          <dbl>
1         4819.

The Armed Forces Pacific (AP) is the territory with the fewest employees:

Code
summarize(state_totals,min(TOTAL))
# A tibble: 1 × 1
  `min(TOTAL)`
         <dbl>
1            1

And Texas is the territory with the most employees:

Code
summarize(state_totals,max(TOTAL))
# A tibble: 1 × 1
  `max(TOTAL)`
         <dbl>
1        19839

Standard deviation of employees is:

Code
sd(state_totals$TOTAL)
[1] 4781.829

Explain and Interpret

I chose the states because I found some of the totals for each state surprising, based on what I know about the size of those states’ populations.