Search code examples
rstringfor-loopnonsequential

R- For loop for non-sequential list


This is probably a simple question, but I'm struggling finding a way to do the equivalent of "for (i in 1:10){ do something}" but with a list of strings. For example:

given a list of strings a = ("Joe", "John", "George") I'd want to do the following:

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

and have the function traverse through the list of names and hit the url with each name. Thanks.


Solution

  • You'd go with for (i in 1:length(a)) { etc } but yeah getting your head around apply functions is generally preferable for speed reasons.