Using a function in R to scrape website, returning "subscript out of bounds" error
<p>I am trying to scrape player data from the Baseball Reference website, using a function to loop through multiple years (variable "year") for each player notated by "playerid."</p>

<pre><code>library(plyr)
library(XML)

fetch_stats &lt;- function(playerid, year) {
  url &lt;- paste0("http://www.baseball-reference.com/players/gl.cgi?id=",playerid,"&amp;t=b&amp;year=",year)
  data &lt;- readHTMLTable(url, stringsAsFactors = FALSE)
  data &lt;- data[[3]]
  data$Year &lt;- year
  data$PlayerId &lt;- playerid
  data
}
</code></pre>

<p>This function works perfectly well when it is applied to a single year's worth of data, as seen here:</p>

<pre><code>AdrianGonzales &lt;- ldply("gonzaad01", fetch_stats, year= 2008, .progress="text")
</code></pre>

<p>However, as soon as I actually use the function to loop through the multiple years in a players career, it always spits out the following error:</p>

<pre><code>AdrianGonzales &lt;- ldply("gonzaad01", fetch_stats, year= 2009:2004, .progress="text")

Error in data[[3]] : subscript out of bounds
In addition: Warning message:
XML content does not seem to be XML: 'http://www.baseball-   reference.com/players/gl.cgi?id=gonzaad01&amp;t=b&amp;year=2009
http://www.baseball-reference.com/players/gl.cgi?id=gonzaad01&amp;t=b&amp;year=2008
http://www.baseball-reference.com/players/gl.cgi?id=gonzaad01&amp;t=b&amp;year=2007
http://www.baseball-reference.com/players/gl.cgi?id=gonzaad01&amp;t=b&amp;year=2006
http://www.baseball-reference.com/players/gl.cgi?id=gonzaad01&amp;t=b&amp;year=2005
http://www.baseball-reference.com/players/gl.cgi?id=gonzaad01&amp;t=b&amp;year=2004'
</code></pre>

<p>From what I have been able to find, the "subscript out of bounds" error happens when you exceed the limits of a defined dataset within R.  For this particular function, I may just be dumb, but I don't see how that would apply in this case- or why it would work for a single year, but not for several at a time.</p>

<p>I'm open to any and all suggestions.  Thanks ahead of time.</p>