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
I slightly changed your brackets and rates parameters. You can solve this problem using a while loop.
income_tax <- function(income,
brackets = c(18200,37001,80000,180000),
rates = c(.19,.325,.37,.45))
{
nbrackets <- length(brackets)
if(income<=brackets[1])
return(0)
i <- 2
cumtax <- 0
while(i <= nbrackets) {
if(income > brackets[i-1] && income < brackets[i])
return(cumtax + rates[i-1]*(income-brackets[i-1]))
else
cumtax <- cumtax + rates[i-1]*(brackets[i]-brackets[i-1])
i <- i + 1
}
# income > brackets[nbrackets]
cumtax + rates[nbrackets] * (income - brackets[nbrackets])
}
incomes <- seq(0,200000,25000)
round(sapply(incomes,income_tax),0)
# [1] 0 1292 7797 15922 24947 34197 43447 52697 63547