[英]How to use map2 with vectors of unequal length
我正在嘗試計算收入在$ 1到$ 200,000之間且應增加$ 100(2000值)的應交所得稅。
我搜集了有關稅率的信息,得出了34個數據框的列表。
我有一個根據收入和適用稅率計算應納稅額的函數。
使用該函數,我想產生一個顯示應納稅額的向量:
如果可以在數據幀/小標題中返回此輸出,那就太好了。
#This scrapes the website of the tax administrator and returns a list of tidy data frames showing tax rates for income years between 2016 and 1983
url <- "https://www.ato.gov.au/Rates/Individual-income-tax-for-prior-years/"
pit_sch <- url %>%
read_html() %>%
html_table() %>%
setNames(., url %>%
read_html() %>%
html_nodes("caption") %>%
html_text()) %>%
map(.%>%
mutate(`Tax on this income` = gsub(",", "", `Tax on this income`),
cumm_tax_amt = str_extract(`Tax on this income`, "(?<=^\\$)\\d+") %>% as.numeric(),
tax_rate = str_extract(`Tax on this income`, "\\d+.(\\d+)?(?=(\\s+)?c)") %>% as.numeric(),
threshold = str_extract(`Tax on this income`, "(?<=\\$)\\d+$") %>% as.numeric()
)
) %>%
map(~drop_na(.x, threshold)) %>%
map(function(x) { mutate_each(x, funs(replace(., is.na(.), 0))) })
#Defining income
income <- seq(from = 1, to = 200000, by = 100)
#The function for calculating tax payable
tax_calc <- function(data, income) {
i <-tail(which(income >= data[, 5]), 1)
if (length(i) > 0)
return(((income - data[i,5]) * (data[i,4]/100)) + data[i,3])
else
return(0)
}
> map2(pit_sch, income, tax_calc)
Error: Mapped vectors must have consistent lengths:
* `.x` has length 34
* `.y` has length 2000
enter code here
為了正確地區分不同的income
和計算該income
年份。 我建議讓tax_calc
函數返回一個tibble
與income
和tax
的計算。
library(tidyverse)
tax_calc <- function(data, income) {
i <-tail(which(income >= data[, 5]), 1)
if (length(i) > 0)
return(tibble(income = income,
tax = (income - data[i,5]) * (data[i,4]/100) + data[i,3]))
else
return(tibble(income = income, tax = 0))
}
既然你想tax_calc
的所有income
為每個pit_sch
可以使用
map(pit_sch,~map_df(income, tax_calc, data = .)) %>% bind_rows(., .id = "id")
檢查它的tail(income)
我們得到
map(pit_sch,~map_df(tail(income), tax_calc, data = .)) %>% bind_rows(., .id = "id")
# A tibble: 204 x 3
# id income tax
# <chr> <dbl> <dbl>
# 1 Resident tax rates for 2016-17 199401 62962.
# 2 Resident tax rates for 2016-17 199501 63007.
# 3 Resident tax rates for 2016-17 199601 63052.
# 4 Resident tax rates for 2016-17 199701 63097.
# 5 Resident tax rates for 2016-17 199801 63142.
# 6 Resident tax rates for 2016-17 199901 63187.
# 7 Resident tax rates for 2015-16 199401 63277.
# 8 Resident tax rates for 2015-16 199501 63322.
# 9 Resident tax rates for 2015-16 199601 63367.
#10 Resident tax rates for 2015-16 199701 63412.
# … with 194 more rows
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.