我正在嘗試編寫一個循環來執行readHTMLTable()
,通過公式提供的連續日期列表。我已成功導入日期之間的所有數據。但是,該數據並不包含日期列,因此使用我提供循環的日期序列,我希望循環讀取HTML表格,然後添加一個新列以及用於該迭代的日期。在日期列表中使用readHTMLTable並使用數據創建新日期列
這是我到目前爲止有:
library(XML)
library(RCurl)
library(plyr)
# create the days
x <- seq(as.Date("2015-04-10"), as.Date("2015-04-15"), by = "day")
# create a url template for sprintf()
utmp <- "http://www.basketball-reference.com/friv/dailyleaders.cgi?month=%d&day=%d&year=%d"
# convert to numeric matrix after splitting for year, month, day
m <- do.call(rbind, lapply(strsplit(as.character(x), "-"), type.convert))
# create the list to hold the results
tables <- vector("list", length(m))
# get the tables
for(i in seq_len(nrow(m))) {
# create the url for the day and if it exists, read it - if not, NULL
tables[[i]] <- if(url.exists(u <- sprintf(utmp, m[i, 2], m[i, 3], m[i, 1])))
readHTMLTable(u, stringsAsFactors = FALSE)
else NULL
}
data <- ldply(tables,data.frame)
所以基本上,我想我的最終數據幀以特色m
一個名爲像data$Date
新列。
感謝您的幫助,如果您需要澄清,請告訴我!
如果循環中沒有'Sys.sleep',那麼您違反了網站的[服務條款](http://www.sports-reference.com/termsofuse.shtml)。 – hrbrmstr