【发布时间】:2018-03-07 12:32:10
【问题描述】:
当我尝试在以下代码中运行时,我收到错误消息“as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) 中的错误:无法强制类”“try-错误"" 到 data.frame "
我正在使用 try 函数跳过不工作的 LINKS 并继续循环,但这没有发生。有人可以帮我解决这个问题吗
base_url <- c("https://www.sec.gov/Archives/edgar/data/1409916/000162828017002570/exhibit211nobilishealthcor.htm",
"https://www.sec.gov/Archives/edgar/data/1300317/000119312507128181/dex211.htm",
"https://www.sec.gov/Archives/edgar/data/1453814/000145381417000063/subsidiariesoftheregistran.htm",
"https://www.sec.gov/Archives/edgar/data/25743/000138713117001111/ex21-1.htm",
"https://www.sec.gov/Archives/edgar/data/880631/000119312517065534/d280058dex211.htm",
"https://www.sec.gov/Archives/edgar/data/1058290/000105829017000008/ctshexhibit21112312016.htm",
"https://www.sec.gov/Archives/edgar/data/1031927/000141588916005383/ex21-1.htm",
"https://www.sec.gov/Archives/edgar/data/1358071/000135807118000008/cxoexhibit211.htm",
"https://www.sec.gov/Archives/edgar/data/904979/000090497918000006/exhibit211q4fy17listofsubs.htm",
"https://www.sec.gov/Archives/edgar/data/41296/000094420901500099/dex21.txt",
"https://www.sec.gov/Archives/edgar/data/808461/000080846117000024/gciexhibit21-1123116.htm",
"https://www.sec.gov/Archives/edgar/data/1101026/000107878213000519/f10k123112_ex21.htm",
"https://www.sec.gov/Archives/edgar/data/932372/000141588915000759/ex21-1.htm"
)
df <- lapply(base_url,function(u){
try({
html_obj <- read_html(u)
draft_table <- html_nodes(html_obj,'table')
cik <- substr(u,start = 41,stop = 47)
draft1 <- html_table(draft_table,fill = TRUE)
final <- c(cik,draft1)
},silent = TRUE)
})
require(reshape2)
data <- melt(df)
data <- as.data.frame(data,row.names = NULL)
data <- data[,1:2]
names(data) <- c("CIK","Company")
data2 <- transform(data, CIK = na.locf(CIK ))
【问题讨论】:
-
尝试使用
tryCatch并在出错时返回例如空数据框或 null。你的melt也不起作用,我猜你必须先dplyr::bind_rows()。 -
你能帮我写代码吗?
标签: r