Last active
November 25, 2019 19:11
-
-
Save pmarkun/0fd198893e60ced2a289bc6eaf40fc0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pay_taxes <- function(income, brackets){ | |
| # for each bracket, subtract x reais from income | |
| taxes_total <- 0 | |
| income_left <- income | |
| for(i in 1:nrow(brackets)){ | |
| # get taxed reais and tax rate | |
| reais_taxed <- brackets[i,]$reais - | |
| ifelse(i==1,0,brackets[i-1,]$reais) | |
| rate_of_tax <- brackets[i,]$tax_rate | |
| # if you're at the top marginal, reset the Inf to = remaining income | |
| reais_taxed <- ifelse(reais_taxed == Inf, income_left, reais_taxed) | |
| # if residual income <0, set taxed reais = remaining income | |
| reais_taxed <- ifelse(income_left - reais_taxed < 0, income_left, reais_taxed) | |
| # calculate taxes paid | |
| taxes_paid <- reais_taxed * rate_of_tax | |
| # calculate residual income | |
| taxes_total <- ifelse(income_left >= 0, taxes_total + taxes_paid, taxes_total) | |
| income_left <- income_left - reais_taxed | |
| } | |
| #return(data.frame(income,taxes_total,on_paper=rate_of_tax)) | |
| return(taxes_total) | |
| } | |
| teste <- data.frame(SAL_CONTRIB = c(1163.55, 2000, 4000, 5000, 7000, 8000, 15000, 30000)) | |
| faixas <- data.frame(reais = c(1163.55,2000,3000,5839.45,10000,20000, Inf), | |
| tax_rate = c(7.5, 9, 12, 14, 14.5, 16.5, 19)/100) | |
| teste$imposto <- pay_taxes(teste$SAL_CONTRIB, faixas) | |
| # pay_taxes(teste$SAL_CONTRIB[8], faixas) == 4833.35 | |
| # teste$imposto[8] == 2933.35 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment