- A+
I am trying to get the host of an IP address from a list of strings.
ips <- c('140.112.204.42', '132.212.14.139', '31.2.47.93', '7.112.221.238')
I want to get the first 2 digits from the ips. output:
ips <- c('140.112', '132.212', '31.2', '7.112')
This is the code that I wrote to convert them:
cat(unlist(strsplit(ips, "//.", fixed = FALSE))[1:2], sep = ".")
When I check the type of individual ips in the end I get something like this:
140.112 NULL
Not sure what I am doing wrong. If you have some other ideas completely different from this that is completely fine too.
With sub
:
ips <- c('140.112.204.42', '132.212.14.139', '31.2.47.93', '7.112.221.238') sub('//.//d+//.//d+$', '', ips) # [1] "140.112" "132.212" "31.2" "7.112"
With str_extract
from stringr
:
library(stringr) str_extract(ips, '^//d+//.//d+') # [1] "140.112" "132.212" "31.2" "7.112"
With strsplit
+ sapply
:
sapply(strsplit(ips, '//.'), function(x) paste(x[1:2], collapse = '.')) # [1] "140.112" "132.212" "31.2" "7.112"
With read.table
+ apply
:
apply(read.table(textConnection(ips), sep='.')[1:2], 1, paste, collapse = '.') #[1] "140.112" "132.212" "31.2" "7.112"
Notes:
-
sub('//.//d+//.//d+$', '', ips)
:i.
//.//d+//.//d+$
matches a literal dot, a digit one or more times, a literal dot again, and a digit one or more times at the end of the stringii.
sub
removes the above match from the string -
str_extract(ips, '^//d+//.//d+')
:i.
^//d+//.//d+
matches a digit one or more times, a literal dot and a digit one or more times in the beginning of the stringii.
str_extract
extracts the above match from the string -
sapply(strsplit(ips, '//.'), function(x) paste(x[1:2], collapse = '.'))
:i.
strsplit(ips, '//.')
splits eachip
using a literal dot as the delimiter. This returns a list of vectors after the splitii. With
sapply
,paste(x[1:2], collapse = '.')
is applied to every element of the list, thus taking only the first two numbers from each vector, and collapsing them with a dot as the separator.sapply
then coerces the list to a vector, thus returning a vector of the desired ips. -
apply(read.table(textConnection(ips), sep='.')[1:2], 1, paste, collapse = '.')
:i.
read.table(textConnection(ips), sep='.')[1:2]
treatsips
as text input and reads it in with dot as a delimiter. Only taking the first two columns.ii.
apply
enablespaste
to be operated on each row, and collapses with a dot.