Challenge 9

challenge_9
Creating a function
Author

Noah Dixon

Published

July 4, 2023

library(tidyverse)
library(ggplot2)

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

Read Data

Using the read.csv function we can read the railroad_2012_clean_county data into a data frame.

railroad <- read.csv("_data/railroad_2012_clean_county.csv")
railroad

Create Function

Now, we can create a function to calculate summary statistics for a variable.

statsFunction <- function(x) {
  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"