[R] IP-Address

David Locke dlocke at opendatagroup.com
Wed Jun 17 23:30:09 CEST 2009


Perhaps it would help to remember that dotted quad notation is
shorthand for an integer value.  If an IP address is a.b.c.d, you can
also represent it as a*16777216 + b*65536 +c*256 +d.

If you can convert IP addresses to and from integers then it should be
trivially easy to find the bounds of the range.

David Locke
Open Data Group

On Wed, Jun 17, 2009 at 3:33 PM, edwin<edwin7 at web.de> wrote:
> Hi all,
>
>
> Sorry, David has just told my that it was a mistake in my example (Thanks David). I had a wrong idea.
>
>
> The right idea is: make a ip range, when the number increament without an gap (and with maximum number: 255, see example down).
>
>
>
> In case my initial example would be:
>
>
>
>> 162.131.58.1
>  > 162.131.58.2
>  > 162.131.58.3
>  > 162.131.58.4
>  > 162.131.58.5
>  > 162.131.58.6
>  The Range is: 162.131.58.1 - 162.131.58.6
>
>
>
>> 162.132.58.20
>  162.132.58.20 (no range)
>
>
>> 162.252.20.21
>  162.252.20.21 (no range)
>
>
>> 162.254.20.22
>  162.254.20.22 (no range)
>
>
>> 163.253.7.23
>  163.253.7.23 (no range)
>
>
>> 163.253.20.25
>  163.253.20.25 (no range)
>
>
>> 161.138.45.226
>  161.138.45.226 (no range)
>
>
> Another example:
>  >162.131.58.1
>  >162.131.58.2
>  >162.131.58.3
>  >162.131.58.4
>  >162.131.58.5
>  >162.131.58.6
>  ....
>  .... (here alll ips from 162.131.58.7 until 162.131.58.253, I just don't write it all down=))
>  ....
>  >162.131.58.254
>  >162.131.58.255 (hier: the max ist 255)
>  >162.131.59.1 (attention: instead 58, now 59)
>  >162.131.59.2
>  >162.131.59.3
>  The range for those ips are:
>  162.131.58.1-162.131.59-3
>
>
>
>
> I hope this example helps. Any suggestion?
>
>
>
>
> Kind regards,
>
>
> Eddie
>
>
>
>> Hi Peter,
>>
>>
>> I hope you could help. I am stuck with this. The last problem I have is:
>>
>> I have table like:
>>
>> id rank color status ip
>> 138 29746 yellow yes 162.131.58.1
>> 138 29746 yellow  yes  162.131.58.2
>> 138 29746 yellow yes  162.131.58.3
>> 138 29746 yellow yes  162.131.58.4
>> 138 29746 yellow yes 162.131.58.5
>> 138 29746 yellow yes  162.131.58.6
>> 138 29746 yellow  yes 162.132.58.20
>> 138 29746 yellow yes  162.252.20.21
>> 138 29746 yellow yes  162.254.20.22
>> 138 29746 yellow yes  163.253.7.23
>> 138 29746 yellow yes  163.253.20.25
>> 138 29746 yellow yes  161.138.45.226
>>
>> How can i make this to a range:
>>
>> 138 29746 yellow yes 162.131.58.1-162.131.58.6
>> 138 29746 yellow  yes 162.132.58.20-163.253.7.23
>> 138 29746 yellow yes  162.252.20.21-162.254.20.22
>> 138 29746 yellow yes  163.253.20.25-161.138.45.226
>>
>>
>>
>> Kind regards,
>>
>> Eddie
>>
>> > edwin7 at web.de wrote:
>> > > Hi,
>> > >
>> > >
>> > > Unfortunately, they can't handle NA. Any suggestion? Some row for Ip
>> > > don't have ip address. This cause an error/ wrong result.
>> >
>> > A quick fix could be to substitute "..." or "0.0.0.0" for the "NA"
>> > entries. (Use something like
>> >
>> > ipch <- as.character(df$ip)
>> > ipch[is.na(df$ip)] <- "..."
>> > connection <- textConnection(ipch)
>> >
>> > )
>> >
>> > > Eddie
>> > >
>> > >> library(gsubfn)
>> > >> library(gtools)
>> > >> library(rbenchmark)
>> > >>
>> > >> n <- 10000
>> > >> df <- data.frame(
>> > >> a = rnorm(n),
>> > >> b = rnorm(n),
>> > >> c = rnorm(n),
>> > >> ip = replicate(n, paste(sample(255, 4), collapse='.'), simplify=TRUE)
>> > >> )
>> > >>
>> > >> res <- benchmark(columns=c('test', 'elapsed'), replications=10,
>> > >
>> > > order=NULL,
>> > >
>> > >> peda = {
>> > >> connection <- textConnection(as.character(df$ip))
>> > >> o <- do.call(order, read.table(connection, sep='.'))
>> > >> close(connection)
>> > >> df[o, ]
>> > >> },
>> > >>
>> > >> peda2 = {
>> > >> connection <- textConnection(as.character(df$ip))
>> > >> dfT <- read.table(connection, sep='.', colClasses=rep("integer",
>> > >> 4), quote="", na.strings=NULL, blank.lines.skip=FALSE)
>> > >> close(connection)
>> > >> o <- do.call(order, dfT)
>> > >> df[o, ]
>> > >> },
>> > >>
>> > >> hb = {
>> > >> ip <- strsplit(as.character(df$ip), split=".", fixed=TRUE)
>> > >> ip <- unlist(ip, use.names=FALSE)
>> > >> ip <- as.integer(ip)
>> > >> dim(ip) <- c(4, nrow(df))
>> > >> ip <- 256^3*ip[1,] + 256^2*ip[2,] + 256*ip[3,] + ip[4,]
>> > >> o <- order(ip)
>> > >> df[o, ]
>> > >> },
>> > >>
>> > >> hb2 = {
>> > >> ip <- strsplit(as.character(df$ip), split=".", fixed=TRUE)
>> > >> ip <- unlist(ip, use.names=FALSE)
>> > >> ip <- as.integer(ip);
>> > >> dim(ip) <- c(4, nrow(df))
>> > >> o <- sort.list(ip[4,], method="radix", na.last=TRUE)
>> > >> for (kk in 3:1) {
>> > >> o <- o[sort.list(ip[kk,o], method="radix", na.last=TRUE)]
>> > >> }
>> > >> df[o, ]
>> > >> }
>> > >> )
>> > >>
>> > >> print(res)
>> > >>
>> > >> test elapsed
>> > >> 1 peda 4.12
>> > >> 2 peda2 4.08
>> > >> 3 hb 0.28
>> > >> 4 hb2 0.25
>> > >>
>> > >>
>> > >> On Sun, May 31, 2009 at 12:42 AM, Wacek Kusnierczyk
>> > >>
>> > >> <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
>> > >> > edwin Sendjaja wrote:
>> > >> >> Hi VQ,
>> > >> >>
>> > >> >> Thank you. It works like charm. But I think Peter's code is faster.
>> > >
>> > > What
>> > >
>> > >> >> is the difference?
>> > >> >
>> > >> > i think peter's code is more r-elegant, though less generic.  here's
>> > >> > a quick test, with not so surprising results.  gsubfn is implemented
>> > >> > in r, not c, and it is painfully slow in this test. i also added
>> > >> > gabor's suggestion.
>> > >> >
>> > >> >    library(gsubfn)
>> > >> >    library(gtools)
>> > >> >    library(rbenchmark)
>> > >> >
>> > >> >    n = 1000
>> > >> >    df = data.frame(
>> > >> >       a=rnorm(n),
>> > >> >       b = rnorm(n),
>> > >> >       c = rnorm(n),
>> > >> >       ip = replicate(n, paste(sample(255, 4), collapse='.'),
>> > >> > simplify=TRUE))
>> > >> >    benchmark(columns=c('test', 'elapsed'), replications=10,
>> > >> > order=NULL, peda={
>> > >> >          connection = textConnection(as.character(df$ip))
>> > >> >          o = do.call(order, read.table(connection, sep='.'))
>> > >> >          close(connection)
>> > >> >          df[o, ] },
>> > >> >       waku=df[order(gsubfn(perl=TRUE,
>> > >> >          '[0-9]+',
>> > >> >          ~ sprintf('%03d', as.integer(x)),
>> > >> >          as.character(df$ip))), ],
>> > >> >       gagr=df[mixedorder(df$ip), ] )
>> > >> >
>> > >> >    # peda 0.070
>> > >> >    # waku 7.070
>> > >> >    # gagr 4.710
>> > >> >
>> > >> >
>> > >> > vQ
>> > >> >
>> > >> > ______________________________________________
>> > >> > R-help at r-project.org mailing list
>> > >> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > >> > PLEASE do read the posting guide
>> > >> > http://www.R-project.org/posting-guide.html and provide commented,
>> > >> > minimal, self-contained, reproducible code.
>> > >>
>> > >> ______________________________________________
>> > >> R-help at r-project.org mailing list
>> > >> https://stat.ethz.ch/mailman/listinfo/r-help
>> > >> PLEASE do read the posting guide
>> > >> http://www.R-project.org/posting-guide.html and provide commented,
>> > >
>> > > minimal,
>> > >
>> > >> self-contained, reproducible code.
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list