Challenge 9

challenge_9
Creating a function
Author

Tenzin Latoe

Published

July 18, 2023

library(tidyverse)
library(ggplot2)

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

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

stats <- function (df) {
  stat <- c(
    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.

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

Chose Railroad data set.

stats(railroad$total_employees)
      Mean     Median    Minimum    Maximum 
  87.17816   21.00000    1.00000 8207.00000 

Ran function.