[R] IP-Address
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sun May 31 09:42:28 CEST 2009
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
More information about the R-help
mailing list