Toralf Kirsten <[EMAIL PROTECTED]> 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
~~~~~~~~~~ - ([EMAIL PROTECTED])             FAX: (+45) 35327907

______________________________________________
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to