title: How to generate country names for a large list of IPs using R?
body: <p>I've a dataset that contains 1 column but huge umber of rows. The column contains huge number of public IP addresses. So its possible to get the geolocation from those IPs using sites like (<a href="http://freegeoip.net).I" rel="nofollow">http://freegeoip.net).I</a> want to generate a column of country names which contains the country name for each IP of the rows. Here is my naive approach - </p>
  
  <pre><code>library(XML)
  
  #Import your list of IPs
  ip.addresses &lt;- read.csv("ip-address.csv")
  
  #This is my API
  api.url &lt;- "http://freegeoip.net/xml/"
  
  #Appending API URL before each of the IPs
  api.with.ip &lt;- paste(api.url, ip.addresses$IP.Addresses ,sep="")
  
  #Creating an empty vector for collecting the country names
  country.vec &lt;- c()
  
  #Running a for loop to parse country name for each IP
  for(i in api.with.ip)
  {
      #Using xmlParse &amp; xmlToList to extract IP information
      data &lt;- xmlParse(i)
      xml.data &lt;- xmlToList(data)
  
      #Selecting only Country Name by using xml.data$CountryName
      #If Country Name is NULL then putting NA
      if(is.null(xml.data$CountryName)){
        country.vec &lt;- c(country.vec, NA)
      }
      else{
        country.vec &lt;- c(country.vec, xml.data$CountryName)
      }
  }
  
  #Combining IPs with its corresponding country names into a dataframe
  result &lt;- data.frame(ip.addresses,country.vec)
  colnames(result) &lt;- c("IP Address", "Country")
  
  #Exporting the dataframe as csv file
  write.csv(result, "IP_to_Location.csv")
  </code></pre>
  
  <p>But as I've huge number of rows, my approach using for loop is very slow. How the process can be faster?</p>