Convert IP Addresses to Country Names or Full Location with Geoplotting

Tools for identifying the origins of IP addresses. Includes functions for converting IP addresses to country names, location details (region, city, zip, latitude, longitude), IP codes, binary values, as well as a function for plotting IP locations on a world map. This product includes IP2Location LITE data available from < http://www.ip2location.com> and is is available by Creative Commons Attribution-ShareAlike 4.0 Interational license (CC-BY-SA 4.0).


  • Functions for converting IP addresses (IPv4) into country names or location details in R.

Default database is available by Creative Commons Attribution-ShareAlike 4.0 Interational license (CC-BY-SA 4.0).
This site or product includes IP2Location LITE data available from http://www.ip2location.com.

devtools::install_github("gitronald/IPtoCountry")
library(IPtoCountry)
data(IPs)

Internet Protocol (IP) addresses serve as the feelers upon which the Internet is able to connect computers and servers across great distances and within complicated clusters. These IP addresses come in two forms, IPv4 and IPv6:

  • Example IPv4 address: 180.20.23.162
  • Example IPv6 address: 2001:0db8:0000:0042:0000:8a2e:0370:7334

IPv6 was built to overcome the eventual exhaustion of available IPv4 addresses, but that hasn't largely happened yet, so our focus here in on IPv4.


{r}

         [,1] [,2] [,3] [,4]
ip.split  180   20   23  162
  • By hand:
> Octet1 = 180
> Octet2 = 20
> Octet3 = 23
> Octet4 = 162
> (Octet1 * 256^3) + (Octet2 * 256^2) + (Octet3 * 256^1) + (Octet4 * 256^0)
[1] 3021215650
  • Function:
> IP_integer("180.20.23.162")
[1] 3021215650
> IP_lookup(3021215650)
[1] "China"

  • All of the above in one simple function
  • Powered by data.table
> IP_country("180.20.23.162")
 
[1] China
Levels: China

  • Produces dataframe with country abreviation, country name, region, city, zip code, latitude, longitude, and GMT
  • Requires download of a larger database to operate, will prompt you to download an R package on Github (https://github.com/gitronald/ip2locationData/blob/master/data/ip2location.lite.db11.rda) containing the data (~40 MB) if you haven't used the function before.
> IP_location("180.20.23.162")
 
  abrv country  region    city    zip     lat      long    gmt
1   CN   China Beijing Beijing 100006 39.9075 116.39723 +08:00

  • Enter IPs, get a map with gradient coloring reflecting the percentage of IP addresses originating in each country
> IP_plot(IPs)


  • Generate five random IP addresses
> IP_generator(5)
[1] "125.65.50.53"    "79.250.76.62"    "142.245.152.177" "230.76.201.42"   "107.182.57.171" 

  • IPs to country names
# Test processing speed on sample of 1000 IPs
> library(microbenchmark)
> microbenchmark(IP_country(IPs))
Unit: milliseconds
            expr      min       lq    mean  median       uq      max neval
 IP_country(IPs) 85.27124 87.03638 91.0021 87.6341 89.12941 206.8301   100
  • IPs to full location details
> microbenchmark(IP_location(IPs))
Unit: seconds
             expr      min       lq     mean  median       uq     max neval
 IP_location(IPs) 3.673867 4.228647 4.614923 4.44011 4.993532 6.40974   100

News

IPtoCountry 0.0.1

  • Convert IP Addresses to Country Names or Full Location with Geoplotting

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("IPtoCountry")

0.0.1 by Ronald E. Robertson, 9 months ago


Browse source code at https://github.com/cran/IPtoCountry


Authors: Ronald E. Robertson [cre, aut], Felix W. Tran [aut], Jonathan Mejia [aut], Christine Mourani [aut]


Documentation:   PDF Manual  


GPL-3 license


Imports data.table, ggplot2, dtables, maps, scales, devtools, install.load


See at CRAN