I\'m writing a function to calculate tax owed given a level of income according to Australia\'s marginal tax rates.
I\'ve written a simple version of the function that r
Here's a one-liner that does the trick:
income_tax <-
function(income,
brackets = c(18200, 37000, 80000, 180000, Inf),
rates = c(0, .19, .325, .37, .45)) {
sum(diff(c(0, pmin(income, brackets))) * rates)
}
Perhaps the easiest way to see how/why it works is to play around with the core bit of logic with some simpler parameters, like this:
brackets <- c(1:5, Inf)
diff(c(0, pmin(.35, brackets)))
## [1] 0.35 0.00 0.00 0.00 0.00 0.00
diff(c(0, pmin(3.9, brackets)))
## [1] 1.0 1.0 1.0 0.9 0.0 0.0
diff(c(0, pmin(99, brackets)))
## [1] 1 1 1 1 1 94