[R] sort a data.frame

David Winsemius dwinsemius at comcast.net
Fri May 21 14:56:11 CEST 2010


But you should both be warned that would only be correct for a species  
where there were 24  chromosomes. This would work for humans and  
species with a higher number:

dd[ order(as.numeric(substring(dd$b, 4), substring(dd$b, 4))), ]

-- 
David Winsemius, MD
West Hartford, CT


On May 20, 2010, at 10:24 PM, Jorge Ivan Velez wrote:

> Hi Yuan,
>
> One way would be:
>
> dd[order(factor(substring(dd$b, 4), levels = c(1:22,  
> LETTERS[1:25]))),]
>
> HTH,
> Jorge
>
>
> On Thu, May 20, 2010 at 10:18 PM, Yuan Jian <> wrote:
>
>> it's a excellent solution. I am sorry I missed something in my  
>> question.
>> the column b consists of not only number but also one letter after  
>> "chr",
>> for example chrX, chrY. I want to put them after number but in the  
>> order of
>> ASCII.
>> i.e. chr1<chr2<....<chr9<chr10<...<chr22<chrA<...chrX<chrY
>>
>> if I have dataframe
>> dd <- data.frame(b = c("chr2", "chr1", "chrY", "chr13", "chrX"),
>>      x = c("A", "D", "A", "C", "C"), y = c(8, 3, 9, 9,7),
>>       z = c(1, 1, 1, 2, 8))
>>
>> the expected result
>>
>>       b x y z
>> 1  chr1 D 3 1
>> 2  chr2 A 8 1
>> 3  chr13 C 9 2
>> 4  chrX C 7 8
>> 5  chrY A 9 1
>>
>>
>>
>>
>>
>> --- On *Thu, 20/5/10, Jorge Ivan Velez <jorgeivanvelez at gmail.com>*  
>> wrote:
>>
>>
>> From: Jorge Ivan Velez <jorgeivanvelez at gmail.com>
>> Subject: Re: [R] sort a data.frame
>> To: "Yuan Jian" <jayuan2008 at yahoo.com>
>> Cc: r-help at r-project.org
>> Received: Thursday, 20 May, 2010, 1:31 PM
>>
>>
>> Hi Yuan,
>>
>> Try
>>
>> dd[order(as.numeric(gsub("[^0-9]", "", dd$b))), ]
>>
>> HTH,
>> Jorge
>>
>> On Thu, 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



More information about the R-help mailing list