[R] ordering data frame

Erik Iverson eriki at ccbr.umn.edu
Wed May 5 23:26:51 CEST 2010



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 at 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.



More information about the R-help mailing list