[R] sorting a data.frame using a vector

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Nov 26 18:22:41 CET 2004


Toralf Kirsten <tkirsten at izbi.uni-leipzig.de> writes:

> 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!

Anything wrong with x[y,] ???

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list