library(tidyverse)
library(ggplot2)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Challenge 9
challenge_9
Creating a function
Challenge Overview
Today’s challenge is simple. Create a function, and use it to perform a data analysis / cleaning / visualization task:
Examples of such functions are: 1) A function that reads in and cleans a dataset.
2) A function that computes summary statistics (e.g., computes the z score for a variable).
3) A function that plots a histogram.
That’s it!
Function
<- function (df) {
stats <- c(
stat Mean = mean(df, na.rm = TRUE),
Median = median(df, na.rm = TRUE),
Minimum = min(df, na.rm = TRUE),
Maximum = max(df, na.rm = TRUE)
)return(stat)
}
Created function to calculate mean, median, minimum, and maximum.
<- read_csv("_data/railroad_2012_clean_county.csv") railroad
Chose Railroad data set.
stats(railroad$total_employees)
Mean Median Minimum Maximum
87.17816 21.00000 1.00000 8207.00000
Ran function.