[R] order intervals in a data.frame

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Thu Sep 6 13:49:42 CEST 2007


Paul Smith wrote:
> On 9/6/07, João Fadista <Joao.Fadista at agrsci.dk> wrote:
>   
>> I would like to know how can I order a data.frame with increasing the dat$Interval (dat$Interval is a factor). There is an example below.
>>
>> Original data.frame:
>>
>>     
>>> dat
>>>       
>>                  Interval               Number_reads
>>                   0-100                         685
>>                200-300                         744
>>                100-200                        1082
>>                300-400                        4213
>>
>>
>>     
>>> Desired_dat:
>>>       
>>                  Interval               Number_reads
>>                   0-100                          685
>>                 100-200                       1082
>>                 200-300                         744
>>                 300-400                       4213
>>     
>
> What about
>
> Desired_dat <- dat[match(dat$Interval,sort(dat$Interval)),]
>   
dat[order(dat$Interval),]

would be more to the point, but it is a bit fortuitous that it works at
all (split the first group at 50 and you'll see).

This (or at least something like it) should sort according to left
endpoints:

o <- order(as.numeric(sub("-.*", "", dat$Interval)))
dat[o,]

> ?
>
> Paul
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>   


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list