Here is yet another way (not sure if it has been submitted): > ip <- c("162.131.58.26","2.131.58.16","2.2.58.10","162.131.58.17") > # split it, create matrix and convert to numeric > ip.s <- do.call(rbind, strsplit(ip, '\\.')) > storage.mode(ip.s) <- 'numeric' > # now create index to sort by > ip.sort <- ip.s %*% c(2^24, 2^16, 2^8, 2^0) > ip[order(ip.sort)] [1] "2.2.58.10" "2.131.58.16" "162.131.58.17" "162.131.58.26" >
On Sat, May 30, 2009 at 8:34 PM, edwin Sendjaja <edwin_0...@msn.com> wrote: > > Hi VQ, > > Thank you. It works like charm. But I think Peter's code is faster. What is > the difference? > > > Eddie > > > > > > Date: Fri, 29 May 2009 11:44:15 +0200 > > From: waclaw.marcin.kusnierc...@idi.ntnu.no > > To: p.dalga...@biostat.ku.dk > > CC: r-help@r-project.org > > Subject: Re: [R] IP-Address > > > > Peter Dalgaard wrote: > > > Allan Engelhardt wrote: > > > > > >> IP addresses are very (very!) difficult to parse and sort correctly > > >> because there are all sorts of supported formats. Try to use > something > > >> like PostgreSQL instead: it is already implemented there. But if you > > >> are sure all your data is of the n.n.n.n form, then something along > the > > >> lines of the following should basically work (I have chosen some more > > >> interesting IP addresses for this): > > >> > > >> > > >> a <- data.frame(cbind(id=c(138,138,138,138), > > >> rank=c(29746,29746,29746,29746), > > >> color=c("yellow","red","blue","red"), > > >> status=c("no","yes","yes","no"), > > >> > > >> ip=c("162.131.58.26","2.131.58.16","2.2.58.10","162.131.58.17"))) > > >> a > > >> # id rank color status ip > > >> # 1 138 29746 yellow no 162.131.58.26 > > >> # 2 138 29746 red yes 2.131.58.16 > > >> # 3 138 29746 blue yes 2.2.58.10 > > >> # 4 138 29746 red no 162.131.58.17 > > >> x <- matrix(unlist(lapply(strsplit(as.character(a$ip), ".", > fixed=TRUE), > > >> as.integer)), > > >> ncol=4, byrow=TRUE) > > >> a[order(x[,1],x[,2],x[,3],x[,4]),] > > >> # id rank color status ip > > >> # 3 138 29746 blue yes 2.2.58.10 > > >> # 2 138 29746 red yes 2.131.58.16 > > >> # 4 138 29746 red no 162.131.58.17 > > >> # 1 138 29746 yellow no 162.131.58.26 > > >> > > >> > > >> Getting rid of the conversions including the matrix(unlist) combo is > > >> left as an exercise (it's too hot here....) > > >> > > > > > > Here's one way: > > > > > > con <- textConnection(as.character(a$ip)) > > > o <- do.call(order,read.table(con,sep=".")) > > > close(con) > > > a[o,] > > > > > > > > > > here's another: > > > > library(gsubfn) > > a[order(gsubfn( > > '[0-9]+', > > ~ sprintf('%03d', as.integer(x)), > > as.character(a$ip))),] > > > > vQ > > > > ______________________________________________ > > R-help@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<http://www.r-project.org/posting-guide.html> > > and provide commented, minimal, self-contained, reproducible code. > > _________________________________________________________________ > > --> Für Fotos hier abdrücken <- > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
______________________________________________ R-help@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.