R/Income.Tax.UK_Function.R

Defines functions Income.Tax.UK

Income.Tax.UK <- function(income, k_id = NULL, df, output = 'dataframe') {
  
  require(tidyverse)
  
  #----- Setup
  if (is.null(df)) {
    df <- data.frame(Gross_Income = income)
  } else {
    df <- as.data.frame(df)
    df$Gross_Income <- df[, income]
    if (!is.null(k_id)) {
      df$k_ID <- df[, k_id]
    }
  }
  
  
  #----- Income Threshold / Personal Allowance
  personal_allowance <- 12500
  higher_tax_band <- 50000
  additional_tax_band <- 150000
  
  income_pers_all_threshold <- 100000
  income_pers_all_exclusion <- 125000
  
  #-- Tax Rates
  ds_rates_pers <- tibble(Brackets = c(personal_allowance, higher_tax_band, additional_tax_band, Inf),
                          Rates = c(0, 0.2, 0.4, 0.45))
  
  ds_ni <- tibble(Brackets = c(719*12, 4167*12, Inf),
                  Rates = c(0, 0.12, 0.02))
  
  #-- Pension Contribution
  pension <- 0.05 * (1-0.2) #- 20% relief
  deductions_salary_sacrifice <- 0
  
  #----- Taxable Income
  df$Deductions_Salary_Sacrifice <- df$Gross_Income*deductions_salary_sacrifice
  df$Taxable_Income <- df$Gross_Income - df$Deductions_Salary_Sacrifice
  
  df$Taxable_Income <- ifelse(df$Taxable_Income < 0 | is.na(df$Taxable_Income),
                              0,
                              df$Taxable_Income)
  
  #----- Calculations for each individual
  for (i in 1:nrow(df)) {
    
    if (df$Taxable_Income[i] <= income_pers_all_threshold) {
      tax_brackets <- ds_rates_pers$Brackets
      tax_rates <- ds_rates_pers$Rates
    } else {
      #-- Personal Allowance Reduction
      personal_allowance_deduction <- ((min(df$Taxable_Income[i], income_pers_all_exclusion)  - income_pers_all_threshold)/2)
      
      #-- New Tax Rate for individual
      ds_rates_non_pers <- ds_rates_pers %>% 
        mutate(
          Brackets = ifelse(Brackets < additional_tax_band, Brackets - personal_allowance_deduction, Brackets)
        )
      
      tax_brackets <- ds_rates_non_pers$Brackets
      tax_rates <- ds_rates_non_pers$Rates
    }
    
    #-- Calculate Income Tax
    df$Income_Tax[i] <- ifelse(df$Taxable_Income[i] > 0,
                               sum(
                                 diff(c(0, pmin(df$Taxable_Income[i], tax_brackets))) * tax_rates
                               ),
                               0)
    
    #-- Calculate NI Contribution
    df$NI_Contribution[i] <- sum(
      diff(c(0, pmin(df$Taxable_Income[i], ds_ni$Brackets))) * ds_ni$Rates
    )
    
    
    #-- Calculation Pension Contribution
    df$Pension_Contribution[i] <- df$Taxable_Income[i]*pension
    
    
  }
  
  
  df$Income_Tax <- ifelse(df$Income_Tax < 0,
                          0,
                          df$Income_Tax)
  
  df$NI_Contribution <- ifelse(df$NI_Contribution < 0,
                               0,
                               df$NI_Contribution)
  
  #----- Net Income
  df$Net_Income <- df$Taxable_Income - df$Income_Tax - df$NI_Contribution - df$Pension_Contribution
  
  #----- Fixes
  #-- k_id
  if (is.null(k_id)) {
    df$k_ID <- 1:nrow(df)
  }
  
  #-- NA Values
  df <- df %>% 
    mutate(
      Gross_Income = ifelse(is.na(Gross_Income), 0, Gross_Income),
      Net_Income = ifelse(is.na(Net_Income), 0, Net_Income),
      Taxable_Income = ifelse(is.na(Taxable_Income), 0, Taxable_Income),
      Income_Tax = ifelse(is.na(Income_Tax), 0, Income_Tax),
      NI_Contribution = ifelse(is.na(NI_Contribution), 0, NI_Contribution),
      Pension_Contribution = ifelse(is.na(Pension_Contribution), 0, Pension_Contribution)
    )
  
  #----- Output
  #-- List
  if (output == 'list') {
    ds_output <- list(
      k_ID = df$k_ID,
      Gross_Income = df$Gross_Income,
      Net_Income = df$Net_Income,
      Taxable_Income = df$Taxable_Income,
      Income_Tax = df$Income_Tax,
      NI_Contribution = df$NI_Contribution,
      Pension_Contribution = df$Pension_Contribution,
    )
  }
  
  #-- Dataframe
  if (output == 'dataframe') {
    ds_output <- df %>% 
      select(
        k_ID,
        Gross_Income,
        Net_Income,
        Taxable_Income,
        Income_Tax,
        NI_Contribution,
        Pension_Contribution
      )
  }
  
  return(ds_output)
  
}
Ehsan-F/R-Mixtape documentation built on June 24, 2020, 12:22 a.m.