Code
library(tidyverse)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Sahasra Iyer
August 16, 2022
Today’s challenge is to
Read in one (or more) of the following data sets, available in the posts/_data
folder, using the correct R package and command.
# A tibble: 10 × 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
Add any comments or documentation as needed. More challenging data may require additional code chunks and documentation.
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).
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.
# Function for computing the mode
mode <- function(group_num){
which.max(tabulate(group_num))
}
# Getting mean, median and mode of total_employees
grouped_data_stats <- clean_county_data %>%
group_by(state) %>%
summarise(total_emp = sum(total_employees),
avg_emp = mean(total_employees),
median_emp = median(total_employees),
mode_emp = mode(total_employees),
sd_emp = sd(total_employees),
quant_0.25_emp = quantile(total_employees, 0.25),
quant_0.75_emp = quantile(total_employees, 0.75),
quant_0.5_emp = quantile(total_employees, 0.5),
min_emp = min(total_employees),
max_emp = max(total_employees)) %>%
arrange(desc(sd_emp))
head(grouped_data_stats, n=5)
# A tibble: 5 × 11
state total_emp avg_emp media…¹ mode_…² sd_emp quant…³ quant…⁴ quant…⁵ min_emp
<chr> <dbl> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
1 IL 19131 186. 42 14 829. 14 98 42 1
2 DE 1495 498. 158 62 674. 110 716. 158 62
3 NY 17050 280. 71 11 591. 27 196 71 5
4 CA 13137 239. 61 2 549. 12.5 200. 61 1
5 CT 2592 324 125 26 520. 63.8 231 125 26
# … with 1 more variable: max_emp <dbl>, and abbreviated variable names
# ¹median_emp, ²mode_emp, ³quant_0.25_emp, ⁴quant_0.75_emp, ⁵quant_0.5_emp
# A tibble: 1 × 3
state county total_employees
<chr> <chr> <dbl>
1 IL COOK 8207
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.
---
title: "Challenge 2 Instructions"
author: "Sahasra Iyer"
desription: "Data wrangling: using group() and summarise()"
date: "08/16/2022"
format:
html:
toc: true
code-fold: true
code-copy: true
code-tools: true
categories:
- challenge_2
- railroads
- faostat
- hotel_bookings
---
```{r}
#| label: setup
#| warning: false
#| message: false
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.xls ⭐
- FAOstat\*.csv or birds.csv ⭐⭐⭐
- hotel_bookings.csv ⭐⭐⭐⭐
```{r}
# Read in the railroad dataset
clean_county_data = read_csv("_data/railroad_2012_clean_county.csv")
# Displaying the top 10 rows
head(clean_county_data, n = 10)
```
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).
## The data looks to be collected from the railroad department of US's different counties, with a mapping of the state to each county. It contains the counts of the total employees of each county working in the railroad department. The data seems to be gathered as an effort for conducting a census of the railroad employees. The cases are the total employee counts for each county and the state the county belongs to, and the variables are "state", "county" and "total_employees".
```{r}
#| label: summary
```
## 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.
```{r}
# Function for computing the mode
mode <- function(group_num){
which.max(tabulate(group_num))
}
# Getting mean, median and mode of total_employees
grouped_data_stats <- clean_county_data %>%
group_by(state) %>%
summarise(total_emp = sum(total_employees),
avg_emp = mean(total_employees),
median_emp = median(total_employees),
mode_emp = mode(total_employees),
sd_emp = sd(total_employees),
quant_0.25_emp = quantile(total_employees, 0.25),
quant_0.75_emp = quantile(total_employees, 0.75),
quant_0.5_emp = quantile(total_employees, 0.5),
min_emp = min(total_employees),
max_emp = max(total_employees)) %>%
arrange(desc(sd_emp))
head(grouped_data_stats, n=5)
```
```{r}
# Finding county with maximum employees
filter(clean_county_data, total_employees==max(total_employees))
# Construct boxplot of total employees across all counties
boxplot(grouped_data_stats$total_emp)
```
### 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.
## I chose to group the data by state to get a count of total employees working in the railroad department in each state. From the boxplot, we can observe that the median is somewhere in the range of 3000-500 employees, while the maximum employees are found to be closer to 20000. Further, we have very few outliers (data points that go beyond the interquartile ranges).