library(tidyverse)
library(ggplot2)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Challenge 9
challenge_9
Creating a function
Read Data
Using the read.csv function we can read the railroad_2012_clean_county data into a data frame.
<- read.csv("_data/railroad_2012_clean_county.csv")
railroad railroad
Create Function
Now, we can create a function to calculate summary statistics for a variable.
<- function(x) {
statsFunction print(paste0("Summary Statistics:"))
print(paste0("Maximum: ", max(x)))
print(paste0("Minimum: ", min(x)))
print(paste0("Mean: ", mean(x, na.rm = TRUE)))
print(paste0("Median: ", median(x, na.rm = TRUE)))
print(paste0("Standard Deviation: ", sd(x, na.rm = TRUE)))
}
Using this function we can print summary statistics for the total_employees column of the railroad data frame.
statsFunction(railroad$total_employees)
[1] "Summary Statistics:"
[1] "Maximum: 8207"
[1] "Minimum: 1"
[1] "Mean: 87.178156996587"
[1] "Median: 21"
[1] "Standard Deviation: 283.635890179709"