[R] sorting a data.frame using a vector

Toralf Kirsten tkirsten at izbi.uni-leipzig.de
Fri Nov 26 18:12:22 CET 2004


Hi all,
I'm looking for an efficient solution (speed and memory) for the 
following problem:
Given
- a data.frame x containing numbers of type double
   with nrow(x)>ncol(x) and unique row lables and
- a character vector y containing a sorted order labels

Now, I'd like to sort the rows of the data.frame x w.r.t. the order of 
labels in y.

example:
x <- data.frame(c(1:4),c(5:8))
row.names(x)<-LETTERS[1:4]
y <- c("C","A","D","B")


My current solution is like this:
if(!is.null(y) && is.vector(y)) {
    nObj <- length(y)
    for (i in 1:nObj) {
      sObj <- y[i]
      k <- c(1:nrow(x))[row.names(x)==sObj]
      if (i != k) {
        names <- row.names(x)
        tObj <- row.names(x[i,])
        temp <- x[i,]
        x[i,] <- x[k,]
        x[k,] <- temp
        names[i] <- sObj
        names[k] <- tObj
        row.names(x) <- names
     }
   }
}

But I'm not happy with it because it is not really efficient. Any other 
suggestions are welcome!

Thanks, Toralf




More information about the R-help mailing list