title: Writing a function to separate working vs. error values in R
body: <p>I've been using this awesome code from the Heuristic Andrew <a href="http://heuristically.wordpress.com/2013/05/20/geolocate-ip-addresses-in-r/" rel="nofollow">blog</a></p>
  
  <p>where you can geolocate IP addresses. I want to be able to run the function over a large (24867 IPs) vector of IP addresses. The problem is most of the IP addresses will return the following error:</p>
  
  <pre><code>Error in file(con, "r") : cannot open the connection
  In addition: Warning message:
  In file(con, "r") : cannot open: HTTP status was '404 Not Found'
  </code></pre>
  
  <p>I'm assuming this occurs because the IP address can not be traced. </p>
  
  <p>I want to create a subset of the vector of IP addresses that will give me the location. I think I would need to write another function puts the IP address into a new vector of working IP addresses, or into a different vector of error IP addresses. </p>
  
  <p>I think I would also need to use the <code>try</code> or <code>tryCatch</code> function? </p>
  
  <p>I'm new to programming so I don't know how to write a function. I've looked at examples online but can't quite figure it out. Any advice would be appreciated.</p>
  
  <p><em><strong>EDIT:</em></strong>
  Thanks to @Thomas for pointing me down the right road...</p>
  
  <p>I used the function is.error to determine if <code>try()</code> will give me an error or not. Returns a logical vector:</p>
  
  <pre><code>is.error &lt;- function(x) inherits(x, "try-error")
  </code></pre>
  
  <p>Tried using <code>lapply()</code></p>
  
  <pre><code>&gt; is.error(
  +   try(
  +     lapply(X  = ls_IPaddresses, FUN = freegeoip)
  +     )
  +   )
  Error in file(con, "r") : cannot open the connection
  [1] TRUE
  </code></pre>
  
  <p>The length of ls_IPaddresses is 24867, so I would want a logical vector with the same length so I can subset the logical vector against ls_IPaddresses.</p>