How to use IP2Location in R language?

IP2Location R package

R is a programming language that is widely used in statistical computing. It was created by Ross Ihaka and Robert Gentleman back in year 1993. Many developers had created external packages in order to extend the function in R. These packages can help users on processing and understanding the data. For example, user can use a geolocation package to identify where the IP address is coming from.

In this article, we are going to guide you on how to use IP2Location Geolocation in R. You will learn how to use the IP2Location R package and the IP2Location BIN database to query the geolocation information of an IP address.

Prerequisite

IP2Location R package leverage the IP2Location Python library for geolocation lookup. Hence, you will need to install the Python and IP2Location module before using this R package.

Before getting started, you are required to download and install the following components:

IP2Location LITE database

Installation

To install IP2Location R package, you can just type the following command in your R console:

install.packages(ip2location)

How to query geolocation information?

  1. In your R console, import IP2Location BIN database by using ip2location::open(). For example, ip2location::open(“full_path_to_IP2Location_BIN_database”)
  2. Query the geolocation information for an IP address by using ip2location::get_all(). For example, ip2location::get_all("8.8.8.8")
  3. You will see the results like this:
$ip
[1] "8.8.8.8"

$country_short
[1] "US"

$country_long
[1] "United States"

$region
[1] "California"

$city
[1] "Mountain View"

$isp
[1] "Google LLC"

$latitude
[1] 37.40599

$longitude
[1] -122.0785

$domain
[1] "google.com"

$zipcode
[1] "94043"

$timezone
[1] "-07:00"

$netspeed
[1] "T1"

$idd_code
[1] "1"

$area_code
[1] "650"

$weather_code
[1] "USCA0746"

$weather_name
[1] "Mountain View"

$mcc
[1] "-"

$mnc
[1] "-"

$mobile_brand
[1] "-"

$elevation
[1] "32"

$usage_type
[1] "SES"

$address_type
[1] "A"

$category
[1] "IAB19"

How to plot the IP address(es) on the map?

  1. In your R console, import IP2Location BIN database by using ip2location::open(). For example, ip2location::open(“full_path_to_IP2Location_BIN_database”)
  2. We can then start to plot the IP addresses to the map by supplying a list of the IP addresses to a function called ip2location::plot_map(). For example, ip2location::plot_map(c('8.8.8.8','8.8.6.6')).
  3. You will see a map image like this:
    IP addresses on Map

Conclusion

At the end of this tutorial, you would have learnt how to install and use the IP2Location R package and then perform an IP geolocation lookup. The geolocation information obtained can help to identify the user’s geographical information and use it for data analytics.


IP2Location BIN Database

Explore the latest IP2Location BIN database to retrieves geolocation information.

Was this article helpful?

Related Articles