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]]
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....)
Allan.
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.
>
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
Here is yet another way: library(gtools) DF[mixedorder(DF$ip), ] On Fri, May 29, 2009 at 12:51 AM, edwin Sendjaja <edwin_0712 at msn.com> 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. >