Calculate marginal tax rates using R

后端 未结 3 1386
闹比i
闹比i 2021-02-07 16:26

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

3条回答
  •  广开言路
    2021-02-07 17:12

    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
    

提交回复
热议问题