-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathr_solution.R
40 lines (35 loc) · 1.75 KB
/
r_solution.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
library(dplyr)
library(tidyr)
# Initial solution (12 steps)
res <- read.csv('data.csv') %>%
mutate(Date = paste(Month, Year)) %>%
mutate(Variable = factor(Variable, levels = c('Salary', 'Taxes', 'Bonus'))) %>%
select(-Month, -Year, -Name) %>%
complete(Date = paste(month.abb, 2022), nesting(Variable)) %>%
mutate(Date = factor(Date, levels = paste(month.abb, 2022))) %>%
arrange(Date, Variable) %>%
replace_na(list(Amount = 0)) %>%
pivot_wider(names_from = Date, values_from = Amount) %>%
bind_rows(summarise(., across(where(is.numeric), sum, na.rm = T), across(where(is.factor), ~"TotalBrutto"))) %>%
rowwise() %>%
mutate(`Year Total` = sum(across(-Variable)))
#Second iteration (9 steps)
res <- read.csv('data.csv') %>%
mutate(Date = paste(Month, Year)) %>%
select(-Month, -Year, -Name) %>%
mutate(Variable = factor(Variable, levels = c('Salary', 'Taxes', 'Bonus'))) %>%
mutate(Date = factor(Date, levels = paste(month.abb, 2022))) %>%
complete(Date, nesting(Variable), fill = list(Amount = 0)) %>%
pivot_wider(names_from = Date, values_from = Amount) %>%
bind_rows(summarise(., across(where(is.numeric), sum, na.rm = T), across(Variable, ~"TotalNetto"))) %>%
rowwise() %>%
mutate(`Year Total` = sum(across(-Variable)))
# Third iteration (with janitor), 7 steps!
res <- read.csv('data.csv') %>%
mutate(Date = paste(Month, Year)) %>%
select(-Month, -Year, -Name) %>%
mutate(Variable = factor(Variable, levels = c('Salary', 'Taxes', 'Bonus'))) %>%
mutate(Date = factor(Date, levels = paste(month.abb, 2022))) %>%
complete(Date, nesting(Variable), fill = list(Amount = 0)) %>%
pivot_wider(names_from = Date, values_from = Amount) %>%
janitor::adorn_totals(c("row","col"), name = c('TotalNetto', 'Year Total'))