[R] IP-Address

Erich Neuwirth erich.neuwirth at univie.ac.at
Fri May 29 11:16:33 CEST 2009


normalizedip <- function(ipstring){
  ipsepstring <- strsplit(ipstring,"\\.")[[1]]
  cat(sapply(ipsepstring,function(x)
       sprintf("%03i",as.numeric(x))),sep=".")
}

normalizedip("1.2.3.55")
yields
 "001.002.003.055"
and therefore should allow you to sort in correct order.


edwin Sendjaja wrote:
> Hi,
> 
> Is there any way to sort a tabel with a colum with IP-address?
> 
> table:
> 
> id rank color status ip
> 138 29746 yellow no 162.131.58.26
> 138 29746 red  yes  162.131.58.16
> 138 29746 blue yes  162.131.58.10
> 138 29746 red no  162.131.58.17
> 138 29746 yellow no 162.131.58.14
> 138 29746 red no  162.131.58.13
> 138 29746 yellow  no 162.132.58.15
> 139 29746 green no  162.252.20.69
> 140 29746 red yes  162.254.20.71
> 141 29746 yellow no  163.253.7.153
> 142 31804 green yes  163.253.20.114
> 144 32360 black yes  161.138.45.226
> ....
> 
> 
> Unfortunately, order doesn't work as I want.
> 
> I found an half solusion from John:
> 
> mysort <- function(x){
>   sort.helper <- function(x){
>     prefix <- strsplit(x, "[0-9]")
>     prefix <- sapply(prefix, "[", 1)
>     prefix[is.na(prefix)] <- ""
>     suffix <- strsplit(x, "[^0-9]")
>     suffix <- as.numeric(sapply(suffix, "[", 2))
>     suffix[is.na(suffix)] <- -Inf
>     remainder <- sub("[^0-9]+", "", x)
>     remainder <- sub("[0-9]+", "", remainder)
>     if (all (remainder == "")) list(prefix, suffix)
>     else c(list(prefix, suffix), Recall(remainder))
>     }
>   ord <- do.call("order", sort.helper(x))
>   x[ord]
>    } 
> 
> 
> mysort (data$ip)  captured only the ip-adresse. How can I capture the whole table and sorted?(ID rank color status ip)
> 
> 
> 
> Thank you in advance.
> 
> eddie
> 
> 
> 
> _________________________________________________________________
> 
> 
> 	[[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.
> 
> 

-- 
Erich Neuwirth, University of Vienna
Faculty of Computer Science
Computer Supported Didactics Working Group
Visit our SunSITE at http://sunsite.univie.ac.at
Phone: +43-1-4277-39464 Fax: +43-1-4277-39459




More information about the R-help mailing list