challenge_2
Author

Lai Wei

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 ⭐
  • FAOstat*.csv ⭐⭐⭐
  • hotel_bookings ⭐⭐⭐⭐
Code
library(readxl)
#import the data of State Country in 2012.
State_Country <- read_excel("_data/StateCounty2012.xls",
                            skip = 4,
                            col_names = c("state","delete","county","delete","employees"))%>%
  select(!contains("delete"))%>%
  filter(!str_detect(state,"Total"))
State_Country <- head(State_Country, -2)%>%
  mutate(county = ifelse(state == "CANADA","CANADA",county))
State_Country
# A tibble: 2,931 × 3
   state county               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,921 more rows
# ℹ Use `print(n = ...)` to see more rows

Add any comments or documentation as needed. More challenging data 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
#get the dimension of State_Country data.
dim(State_Country)
[1] 2931    3
Code
#return column names of dataset
colnames(State_Country)
[1] "state"     "county"    "employees"

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.

Code
#the column 'state' can be selected from State_Country
select(State_Country,"state")
# A tibble: 2,931 × 1
   state
   <chr>
 1 AE   
 2 AK   
 3 AK   
 4 AK   
 5 AK   
 6 AK   
 7 AK   
 8 AL   
 9 AL   
10 AL   
# … with 2,921 more rows
# ℹ Use `print(n = ...)` to see more rows
Code
#get data have "MA" in 'state'column
filter(State_Country, state == "MA")
# A tibble: 12 × 3
   state county     employees
   <chr> <chr>          <dbl>
 1 MA    BARNSTABLE        44
 2 MA    BERKSHIRE         50
 3 MA    BRISTOL          232
 4 MA    ESSEX            314
 5 MA    FRANKLIN         113
 6 MA    HAMPDEN          202
 7 MA    HAMPSHIRE         68
 8 MA    MIDDLESEX        673
 9 MA    NORFOLK          386
10 MA    PLYMOUTH         429
11 MA    SUFFOLK          558
12 MA    WORCESTER        310

Explain and Interpret

Be sure to explain why you choose a specific group. Comment on the interpretation of any interesting differences between groups that you uncover. This section can be integrated with the exploratory data analysis, just be sure it is included.