Someone on the R-help mailing list had a data frame with a column containing IP addresses in quad-dot format (e.g. 1.10.100.200). He wanted to sort by this column and I proposed a solution involving strsplit
. But Peter Dalgaard comes up with a much nicer method using read.table
on a textConnection
object:
> a <- data.frame(cbind(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")))
> con <- textConnection(as.character(a$ip))
> o <- do.call(order,read.table(con, sep="."))
> close(con)
> a[o,]
color status ip3 blue yes 2.2.58.10
2 red yes 2.131.58.16
4 red no 162.131.58.17
1 yellow no 162.131.58.26
That is very, very neat! Thank you Peter.