[R] IP-Address

Henrik Bengtsson hb at stat.berkeley.edu
Mon Jun 1 20:06:16 CEST 2009


Feel free to use my code however you want.  If you put in a package I
prefer an LGPL license, but not required.  See R documentation on how
to create a package.

-H

On Mon, Jun 1, 2009 at 9:00 AM, edwin <edwin7 at web.de> wrote:
>
> Maybe you can make a package for something like this. (IP sort,etc). Like in
> Perl, there is a module called Sort::Key::IPv4
> I think, not only me has the same problem for sorting e.g IP?
>
>
> Eddie
>> Not really, just the old saying that any piece of code can be made
>> twice as fast (which often holds true recursively). /Henrik
>>
>> On Sun, May 31, 2009 at 1:58 PM, Wacek Kusnierczyk
>>
>> <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
>> > wow! :)
>> >
>> > vQ
>> >
>> > Henrik Bengtsson wrote:
>> >> 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
>> >
>> > ______________________________________________
>> > 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.
>
>
>




More information about the R-help mailing list