README.md

IPtoCountry

Build Status

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.

Getting Started

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

IP Addresses

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.

How to Determine the Location of an IP Address

Step 1 - Split IP address into four octets
> IP_split("180.20.23.162")
         [,1] [,2] [,3] [,4]
ip.split  180   20   23  162
Step 2 - Calculate IP Integer from Octets
> Octet1 = 180
> Octet2 = 20
> Octet3 = 23
> Octet4 = 162
> (Octet1 * 256^3) + (Octet2 * 256^2) + (Octet3 * 256^1) + (Octet4 * 256^0)
[1] 3021215650
> IP_integer("180.20.23.162")
[1] 3021215650
Step 3 - Lookup IP Integer in a Database
> IP_lookup(3021215650)
[1] "China"

IP_country - Convert IP address to country name

> IP_country("180.20.23.162")

[1] China
Levels: China

IP_location - Convert IP address into detailed location

> 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

IP_plot - Convert IPs to countries and plot on world map

> IP_plot(IPs)

IP_generator - Generate 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" 

Speed

# 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
> 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


Try the IPtoCountry package in your browser

Any scripts or data that you put into this service are public.

IPtoCountry documentation built on May 2, 2019, 6:03 a.m.