Reputation: 1
I need to R to convert IP addresses into Country names (I'm not interested in any other geolocation variables).
Let's say I have the following list of IP addresses (df).
This is my code:
df <- read.table(text="
ip_address
178.10.17.178
157.213.207.99
94.230.235.15
97.103.63.156
248.161.179.64
12.114.230.175
68.68.181.51
59.7.213.18
59.38.90.12
103.213.135.197", header = TRUE)
install.packages('pacman')
# load libraries
pacman::p_load(ip2location, reticulate, tidyverse)
# load IP2Location bin file
ip2location::open("C:/Users/smbc1y19/Downloads/IP2LOCATION-LITE-DB1.BIN/IP2LOCATION-LITE-DB123.BIN")
df <- df |>
mutate(details = map(ip_address, get_all)) |>
unnest_wider(details)
df
This is the output:
# load IP2Location bin file
ip2location::open("/Users/me/Downloads/IP2LOCATION-LITE-DB11.BIN/IP2LOCATION-LITE-DB11.BIN")
df <- df |>
mutate(details = map(ip_address, get_all)) |>
unnest_wider(details)
# df
# A tibble: 10 × 10
ip_address ip country_short country_long
<chr> <chr> <chr> <chr>
1 178.10.17.178 178.10.17.178 DE Germany
2 157.213.207.99 157.213.207.99 US United States of America
3 94.230.235.15 94.230.235.15 UZ Uzbekistan
4 97.103.63.156 97.103.63.156 US United States of America
5 248.161.179.64 248.161.179.64 - -
6 12.114.230.175 12.114.230.175 US United States of America
7 68.68.181.51 68.68.181.51 US United States of America
8 59.7.213.18 59.7.213.18 KR Korea (Republic of)
9 59.38.90.12 59.38.90.12 CN China
10 103.213.135.197 103.213.135.197 CN China
region city latitude longitude zipcode timezone
<chr> <chr> <chr> <chr> <chr> <chr>
1 Hessen Eschborn 50.143517 8.570921 65760 +02:00
2 Ohio Columbus 39.966381 -83.012772 43218 -04:00
3 Toshkent Tashkent 41.264648 69.216270 700011 +05:00
4 Florida Orlando 28.538340 -81.379242 32801 -04:00
5 - - 0.000000 0.000000 - -
6 New Jersey Piscataway 40.554516 -74.460167 08854 -04:00
7 Tennessee Camden 36.058815 -88.102158 38320 -05:00
8 Gyeonggi-do Seongnam 37.420624 127.126717 13118 +09:00
9 Guangdong Foshan 23.026770 113.131477 528000 +08:00
10 Jiangsu Changzhou 31.783331 119.966667 213019 +08:00
plot_map(df$ip_address)
How can I change my code to save just country_long as a new variable in my dataframe?
Upvotes: 0
Views: 74
Reputation: 1
Would a simple select() work? My understanding of your question is that you want your output to contain just the original data, ip_address
, and an extra column, country_long
.
df <- df |>
mutate(details = map(ip_address, get_all)) |>
unnest_wider(details) |>
# Selects the desired columns and leaves out the rest
select(ip_address, country_long)
Here is some documentation on R, specifically linked is the page to do with selecting and renaming columns of a dataframe: https://benwhalley.github.io/just-enough-r/selecting-columns.html
Upvotes: 0