Conceptually, you want to:
- Split your IP by
. delimiter and get the last (4th) match
- Convert the result into an integer for further eval
- Evaluate against a filter (
>=128)
It's a bit tricky to do in R as I'm not aware of any functions that would return exactly first or last match, but with a bit of purrr, nothing is impossible:
library(tidyverse)
IP_LIST <- tibble(
"User" = c("John", "Carl", "Mary",
"Kim", "Jane", "Jessie",
"Peter"),
"IP" = c('172.16.0.15',
'192.168.200.90',
'172.16.2.129',
'198.16.15.254',
'172.25.25.19',
'192.168.25.200',
'192.129.200.10') )
result <- IP_LIST %>%
mutate(split = str_split(IP, "\\."),
num = as.integer(unlist(map(split, ~.[4])))) %>%
filter(num >= 128) %>%
select(-num, -split)
# A tibble: 3 x 2
User IP
<chr> <chr>
1 Mary 172.16.2.129
2 Kim 198.16.15.254
3 Jessie 192.168.25.200
A mutate call may need some explanation. I'll try to come back to this thread and break it down when I have more time. Essentially, it does what I outlined earlier:
-
split splits IP into 4 parts
- In the next call for
num, working inside out, I extract 4th element of the list with purrr::map, unlist the result with unlist, and convert the character into an integer.
- I then filter to leave only rows as per your condition
- Finally, I remove helper columns.
Hope this helps