【发布时间】:2019-06-11 01:40:37
【问题描述】:
问题
我有一个历史税率列表和一个应税收入向量,我需要将它们组合起来,以便计算每年每个收入水平的纳税义务。当我去迭代历史税率和收入时,我收到一条错误消息:
Error: Argument 2 can't be a list containing data frames
对有关如何修改数据或函数调用(如下)以完成迭代的任何建议感兴趣。
数据
pit_sch <- list(`2016` = structure(list(id = c("2016", "2016", "2016", "2016"
), hh_exp_def = c(0.989, 0.989, 0.989, 0.989), `Taxable income` = c("$18,201 – $37,000",
"$37,001 – $80,000", "$80,001 – $180,000", "$180,001 and over"
), `Tax on this income` = c("19c for each $1 over $18200", "$3572 plus 32.5c for each $1 over $37000",
"$17547 plus 37c for each $1 over $80000", "$54547 plus 45c for each $1 over $180000"
), cumm_tax_amt = c(0, 3572, 17547, 54547), tax_rate = c(19,
32.5, 37, 45), threshold = c(18200, 37000, 80000, 180000), real_threshold = c(18402.4266936299,
37411.5267947422, 80889.7876643074, 182002.022244692), real_cumm_tax_amt = c(0,
3611.72901921132, 17742.16380182, 55153.6905965622)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -4L)), `2017` = structure(list(
id = c("2017", "2017", "2017", "2017"), hh_exp_def = c(1,
1, 1, 1), `Taxable income` = c("$18,201 – $37,000", "$37,001 – $87,000",
"$87,001 – $180,000", "$180,001 and over"), `Tax on this income` = c("19c for each $1 over $18200",
"$3572 plus 32.5c for each $1 over $37000", "$19822 plus 37c for each $1 over $87000",
"$54232 plus 45c for each $1 over $180000"), cumm_tax_amt = c(0,
3572, 19822, 54232), tax_rate = c(19, 32.5, 37, 45), threshold = c(18200,
37000, 87000, 180000), real_threshold = c(18200, 37000, 87000,
180000), real_cumm_tax_amt = c(0, 3572, 19822, 54232)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -4L)))
income <- seq(from = 1, to = 100000, by = 100)
尝试
# Defining the function which will calculate tax liability for a given set of tax rates (in pit_sch) and income
nominial_tax_calc <- function(data, income) {
i <-pmax(which(income >= data[, 7]))
if (length(i) > 0)
return(tibble(income = income,
tax = (income - data[i, 7]) * (data[i, 6] / 100) + data[i, 5]))
else
return(tibble(income = income, tax = 0))
}
# Function that results in the error
map(pit_sch,~map_df(income, nominial_tax_calc, data = .))
【问题讨论】: