HW 4 - Part 1

Chunk 2: Create bls_tidy function from provided code.

# import and clean bls data
unemp_orig <- read_csv("bls_unemp_rate.csv", skip = 11, show_col_types = F) |> 
  pivot_longer(cols = Jan:Dec,                      # reshapes data
               names_to = "month", 
               values_to = "value") |>
  filter(!is.na(value)) |>                          # removes missing values
  rename("unemp_rate" = "value")                    # renames quantitative var 


bls_tidy <- function(data_file, skip_num, var_name){

  read_csv(data_file, skip = skip_num, show_col_types = F) |> 
  pivot_longer(cols = Jan:Dec,                      # reshapes data
               names_to = "month", 
               values_to = "value") |>
  filter(!is.na(value)) |>                          # removes missing values
  rename({{var_name}} := "value")                   # renames quantitative var 
  
}


unemp <- bls_tidy(data_file = "bls_unemp_rate.csv", 
                  skip_num = 11, 
                  var_name = "unemp_rate")

HW 4 - Part 2

Chunk 3: Use function to import two datasets.

export_index <- bls_tidy(data_file = "bls_export_index.csv", 
                         skip_num = 10, 
                         var_name = "exp_indx")

import_index <- bls_tidy(data_file = "bls_import_index.csv", 
                         skip_num = 10, 
                         var_name = "imp_indx")

export_index |> pull(exp_indx) |> summary()
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   118.1   122.9   126.3   130.6   133.5   166.7
import_index |> pull(imp_indx) |> summary()
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   117.2   123.2   126.5   129.8   138.3   148.5

HW 4 - Part 3

Chunk 4: Join datasets and create date variable.

export_import <- full_join(export_index, import_index) |>
  mutate(date = ym(paste(Year, month))) |>
  glimpse()
## Joining, by = c("Year", "month")
## Rows: 121
## Columns: 5
## $ Year     <dbl> 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2…
## $ month    <chr> "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep"…
## $ exp_indx <dbl> 134.1, 135.1, 134.4, 133.6, 132.9, 132.8, 132.6, 131.9, 132.4…
## $ imp_indx <dbl> 140.1, 141.3, 141.2, 140.2, 139.4, 138.8, 138.9, 139.4, 139.8…
## $ date     <date> 2013-01-01, 2013-02-01, 2013-03-01, 2013-04-01, 2013-05-01, …

HW 4 - Part 4

Chunk 5: Reshape, format and plot data

exp_imp_plt <- export_import |> 
  select(date, exp_indx, imp_indx) |>
  pivot_longer(cols = exp_indx:imp_indx, names_to = "type", values_to = "value") |>
  mutate(type = factor(type,
                       levels = c("exp_indx", "imp_indx"),
                       labels = c("Export", "Import")))

HW 4 - Part 5

Chunk 6: Create Formatted Plot

(exp_imp_lineplot <- exp_imp_plt |>                                       # replace blank with name of dataset
  ggplot() +
  geom_line(aes(x = date, y = value, color = type), size = 1) +      # complete geom_line command
  
# can opt for different theme
  theme_classic() +                                           

# moves legend to bottom
  theme(legend.position="bottom") +                        

# specifies colors (mnay other options vailable)
  scale_color_manual(values=c("lightblue","blue")) +       
                                                        
# formats x axis to show each year as 4 digits                                       
  scale_x_date(date_breaks = "year", date_labels = "%Y") +

  labs(x = "Date", 
       y = "Index", 
       color = "",
       title = "Import and Export Price Indices", 
       subtitle = "January 2013 - January 2023",
       caption = "Data Source - www.bls.gov"))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.