Challenge 9 Solution

challenge_9
Susannah Reed Poland
Creating a function
Author

Susannah Reed Poland

Published

June 12, 2023

library(tidyverse)
library(ggplot2)
library(lubridate)
library(readxl)

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

Create a function that reads in and cleans a dataset

I chose to create a function that changes a column of year and quarter characters to dates, and removes the old column.

#create a function called "read_debt", f is the file name and by default is false
read_debt<-function(f){
#read in the data file
  d<-read_excel(f)%>%
#change the "year and quarter" to a date format
    mutate(date_orig = parse_date_time(`Year and Quarter`, orders="yq"))%>%
#remove the old column
  select(-`Year and Quarter`)
  return(d)
}

Now we can apply this to the “Debt in Trillions” dataset:

#apply function
debt_date<-read_debt("_data/debt_in_trillions.xlsx")

#check it out!
debt_date

Et voila!