我有两列“名称”和“链接”,分别表示公司名称和废料链接。我该如何放回名称列,以便每一行报废的数据都有一个与公司名称相对应的列?
List.Of.Tabs <- map(pages, ~ {
name <- .x[1]
link <- .x[2]
webpage <- read_html(link)
tbls <- html_nodes(webpage, "table")
tbls_ls <- html_table(tbls,fill = TRUE)
pos1 <- possibly(function(tbls) bind_rows(tbls) %>%
filter_all(any_vars(. %in% c("Ireland", "Japan")))
, otherwise = NA)
pos1(tbls_ls)
结果应该是这样的:
results <- data.frame(subsidiaries = c('Microsoft Japan','Microsoft Ireland'),
country = c('Japan', 'Ireland'),
name = c('Microsoft','Microsoft'))
答案 0 :(得分:1)
根据代码,我们可能需要mutate
(即,在代码的第一行中从已创建的“名称”创建一个“名称”列)
List.Of.Tabs <- map(pages, ~ {
name <- .x[1]
link <- .x[2]
webpage <- read_html(link)
tbls <- html_nodes(webpage, "table")
tbls_ls <- html_table(tbls,fill = TRUE)
pos1 <- possibly(function(tbls) bind_rows(tbls) %>%
filter_all(any_vars(. %in% c("Ireland", "Japan"))) %>%
mutate(name = name) # changed here
, otherwise = NA)
pos1(tbls_ls)
})