R - 向量中每个字符的重复分配

时间:2015-12-13 18:42:46

标签: r loops rvest

我有一个包含30个字符的向量:

tm <- c("CHI","CLE","IND","MIA","TOR","ATL","BOS","ORL","CHO","DET","NYK","WAS","MIL","BRK","PHI","GSW","SAS","OKC","DAL","MEM","LAC","UTA","HOU","MIN","POR","PHO","SAC","DEN","NOP","LAL")

我正在尝试为每个角色创建一个新对象。该对象是由以下 read_html 函数生成的列表:

html_TOR <- read_html("http://www.basketball-reference.com/teams/TOR/2016/gamelog/#tgl_advanced::none")

如何创建一个循环,用 tm 向量中的每个字符替换函数中指定对象和URL中的“TOR”?

这是我可怕的尝试......善待。

for (i in tm) {html_[i] <- read_html("http://www.basketball-reference.com/teams/[i]/2016/gamelog/#tgl_advanced::none")}

1 个答案:

答案 0 :(得分:0)

将结果保存在列表而不是单独的对象中,并使用create all urls:

html <- list()
for (i in 1:length(tm)) {
    html[[i]] <- read_html(paste("http://www.basketball-reference.com/teams/",tm[i],"/2016/gamelog/#tgl_advanced::none", sep="")
}