phoebe kong wrote:
Hi all,

I have problem in ordering data frame. Could anyone help me?

x
     [,1] [,2] [,3]
[1,] "A"  "1"  "2"
[2,] "G"  "3"  "2"
[3,] "E"  "2"  "3"

y
     [,1] [,2] [,3]
[1,] "G"  "3"  "3"
[2,] "A"  "3"  "3"
[3,] "E"  "3"  "3"

Are these really data.frames? They looks like matrices. You do not provide ?dput output.


I would like to order data frame x by the order of column 1 of data frame y,
as follow,

     [,1] [,2] [,3]
[1,] "G"  "3"  "2"
[2,] "A"  "1"  "2"
[3,] "E"  "2"  "3"


My example uses data.frames.  ?match and ?order are the important functions.

df1 <- data.frame(a = sample(LETTERS[1:5]), b = rnorm(5))
df2 <- data.frame(a = sample(LETTERS[1:5]), b = rnorm(5))

df1[order(match(df1$a, df2$a)), ]









Thanks,
Phoebe

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to