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.