[R] sort a data.frame

Nikhil Kaza nikhil.list at gmail.com
Thu May 20 15:19:55 CEST 2010


Try this.

dd[order(gsub("chr","",dd$b)),]

You need regular expressions if chr is not the only characterstring  
that is prepended to the numbers.
look for
?strsplit


Nikhil Kaza
University of North Carolina
nikhil.list at gmail.com



On May 20, 2010, at 8:28 AM, Yuan Jian wrote:

> Hello,
>
> I have a dataframe:
> dd <- data.frame(b = c("chr2", "chr1", "chr15", "chr13"),
>       x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9),
>        z = c(1, 1, 1, 2))
>
>> dd
>       b x y z
> 1  chr2 A 8 1
> 2  chr1 D 3 1
> 3 chr15 A 9 1
> 4 chr13 C 9 2
>
> Now I want to sort them according column "b", but only its number is  
> considered:
>       b x y z
> 1  chr1 D 3 1
> 2 chr13 C 9 2
> 3 chr15 A 9 1
> 4  chr2 A 8 1
>
> thanks
> jian
>
>
>
>
>
>
> 	[[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