1

I am trying to calculate after-tax income for each household in a data frame like this:

     id  hhinc  
1     1  53880  
2     2  49501  
3     3  37525  
4     4  28791   
5     5  91049    
6     6 133000   
7     7  12299        
8     8  23000   
9     9  58100   
10   10   9764    

where hhinc is household income.

I then created the following function to calculate the taxes paid by each household:


taxpaid = function(hhinc) { 
  if (hhinc > 0 & hhinc <= 19999) {tax = 0} 
  else if (hhinc > 20000 & hhinc <= 49999) {tax = (hhinc - 20000)*.15} 
  else if (hhinc > 50000 & hhinc <= 199999) {tax = 4499.85 + ((hhinc - 50000)*.25)} 
  else if (hhinc > 200000 & hhinc <= 999999) {tax <- 37499.75 + ((hhinc - 200000)*.39)} 
  else if (hhinc > 1000000) {tax <- 311999.61 + ((hhinc - 1000000)*.85)}
  return(tax)
}

Since this function only works for a scalar input, I vectorized the function:

taxpaid_vec = Vectorize(taxpaid, vectorize.args = "hhinc")

However, when I use this function to calculate the taxes paid, I receive non-numeric outputs. I therefore cannot subtract the taxes paid from each household's income to determine the after-tax income. I would like to know how to fix my code so that get a numeric output for taxes paid.

1 Answer 1

3

Replace if/else to ifelse to make your function vectorized.

taxpaid = function(hhinc) { 
   ifelse(hhinc > 0 & hhinc <= 19999, 0,
    ifelse(hhinc > 20000 & hhinc <= 49999, (hhinc - 20000)*.15, 
     ifelse(hhinc > 50000 & hhinc <= 199999, 4499.85 + ((hhinc - 50000)*.25),
      ifelse(hhinc > 200000 & hhinc <= 999999, 37499.75 + ((hhinc - 200000)*.39), 
       ifelse(hhinc > 1000000, 311999.61 + ((hhinc - 1000000)*.85), NA)))))
}

Apply the function

df$tax_income <- taxpaid(df$hhinc)
df

#   id  hhinc tax_income
#1   1  53880    5469.85
#2   2  49501    4425.15
#3   3  37525    2628.75
#4   4  28791    1318.65
#5   5  91049   14762.10
#6   6 133000   25249.85
#7   7  12299       0.00
#8   8  23000     450.00
#9   9  58100    6524.85
#10 10   9764       0.00

You might also look in to ?dplyr::case_when for handling such nested conditions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.