问题
我需要将一个列表折叠到一个dataframe/tibble中,并将列表名转换为每个观察中的值。
数据
#This chunk generates the list
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))) })我的尝试
这段代码确实创建了我想要的数据,但在我需要的每个观察中不包括列表项的名称。
map_df(pit_sch, `[`, c("Taxable income", "Tax on this income", "cumm_tax_amt", "tax_rate", "threshold"))成功看起来就像
输出应包括与数据相关的列表项的名称:"table_name"、“应税收入”、“对此收入征税”、"cumm_tax_amt“、"tax_rate”、“门槛”。
发布于 2019-06-06 04:15:02
我们可以使用bind_rows与.id一起从list的names中创建一个以“table_name”作为新列的data.frame
library(tidyverse)
out <- bind_rows(pit_sch, .id = 'table_name')https://stackoverflow.com/questions/56470789
复制相似问题