library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)
read_and_clean <- function(path){
debt <- read_excel(path)
debt <- debt %>%
mutate(`Year and Quarter` = yq(`Year and Quarter`)) %>%
rename(Date = `Year and Quarter`)
return(debt)
}
debt <- read_and_clean("challenge_datasets/debt_in_trillions.xlsx")
z_score <- function(vec) {
if (is.numeric(vec)) {
mu <- mean(vec)
s <- sd(vec)
return((vec - mu) / s)
}
}
map_dfr(debt, z_score)# A tibble: 74 × 7
Mortgage `HE Revolving` `Auto Loan` `Credit Card` `Student Loan` Other Total
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 -2.81 -2.22 -1.24 -0.996 -1.59 2.06 -2.61
2 -2.70 -2.07 -1.32 -0.923 -1.58 2.25 -2.53
3 -2.61 -2.00 -1.05 -0.923 -1.57 2.06 -2.43
4 -2.21 -1.73 -0.967 -0.851 -1.56 1.43 -2.13
5 -2.05 -1.52 -0.899 -0.894 -1.54 1.38 -2.01
6 -1.95 -1.21 -0.801 -0.865 -1.53 0.873 -1.91
7 -1.74 -0.728 -0.766 -0.734 -1.38 0.587 -1.69
8 -1.62 -0.389 -0.864 -0.574 -1.34 0.869 -1.57
9 -1.49 -0.114 -0.877 -0.676 -1.30 0.241 -1.48
10 -1.33 0.0965 -0.668 -0.574 -1.27 0.422 -1.31
# ℹ 64 more rows