我正在尝试编写一个循环来执行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