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.
The concat. split function takes a column with multiple values, splits the values into a list or into separate columns, and returns a new data. frame or data. table .
Method: In Python, we can use the function split() to split a string and join() to join a string. the split() method in Python split a string into a list of strings after breaking the given string by the specified separator.
In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.
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 string
ii. 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 string
ii. str_extract
extracts the above match from the string
sapply(strsplit(ips, '\\.'), function(x) paste(x[1:2], collapse = '.'))
:
i. strsplit(ips, '\\.')
splits each ip
using a literal dot as the delimiter. This returns a list of vectors after the split
ii. 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]
treats ips
as text input and reads it in with dot as a delimiter. Only taking the first two columns.
ii. apply
enables paste
to be operated on each row, and collapses with a dot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With