0

这可能是一个简单的问题,但我正在努力寻找一种方法来做相当于“for (i in 1:10){ do something}”但使用字符串列表。例如:

给定一个字符串列表 a = ("Joe", "John", "George") 我想做以下事情:

for (a in "Joe":"George"){
  url <- paste0(http://www.website.com/", a)
  readHTMLTable(url)
}

并让函数遍历名称列表并使用每个名称点击 url。谢谢。

4

2 回答 2

0

出于速度原因,您会选择使用for (i in 1:length(a)) { etc }应用功能,但是通常更可取。

于 2016-10-22T02:56:18.327 回答
0

在 paste0 函数中使用 ""

a = c("Joe", "John", "George")

for (i in 1:length(a)){
  url <- paste0("http://www.website.com/", a)
      readHTMLTable(url)
}

lapply(a, function(x){paste0("http://www.website.com/", x)})
[[1]]
[1] "http://www.website.com/Joe"

[[2]]
[1] "http://www.website.com/John"

[[3]]
[1] "http://www.website.com/George"

sapply(a, function(x){paste0("http://www.website.com/", x)})

Joe                            John                          George 
"http://www.website.com/Joe"   "http://www.website.com/John" "http://www.website.com/George" 
于 2016-10-22T03:57:52.953 回答