首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列出行的名称

列出行的名称
EN

Stack Overflow用户
提问于 2019-06-06 04:11:52
回答 1查看 32关注 0票数 1

问题

我需要将一个列表折叠到一个dataframe/tibble中,并将列表名转换为每个观察中的值。

数据

代码语言:javascript
复制
#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))) })

我的尝试

这段代码确实创建了我想要的数据,但在我需要的每个观察中不包括列表项的名称。

代码语言:javascript
复制
map_df(pit_sch, `[`, c("Taxable income", "Tax on this income", "cumm_tax_amt", "tax_rate", "threshold"))

成功看起来就像

输出应包括与数据相关的列表项的名称:"table_name"、“应税收入”、“对此收入征税”、"cumm_tax_amt“、"tax_rate”、“门槛”。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-06 04:15:02

我们可以使用bind_rows.id一起从listnames中创建一个以“table_name”作为新列的data.frame

代码语言:javascript
复制
library(tidyverse)
out <- bind_rows(pit_sch, .id = 'table_name')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56470789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档