[R] Sorting a data frame by specifying a vector

Bert Gunter gunter.berton at gene.com
Thu Oct 11 20:10:54 CEST 2012


On Thu, Oct 11, 2012 at 10:43 AM, ROLL Josh F <JRoll at lcog.org> wrote:
> Sorry if I wasn't clear.
Actually, my bad -- I didn't read carefully enough.

But the answer is still essentially correct -- just change the
ordering of the levels of Season, which, by default, is alphabetic.

df$Season <- factor(df$Season, lev = c("Summer","Fall","Winter","Spring"))

df <- df[order(df$Season),]

Learn about factors (Read the Intro to R tutorial if you haven't
already). They are very handy (and much despised by some).

-- Bert


The result I am looking for would be something like:
>
> #   Season       Obs
> #1  Summer 0.2141001
> #5  Summer 0.2141001
> #9  Summer 0.2141001
> #13 Summer 0.2141001
> #3    Fall 0.6722337
> #7    Fall 0.6722337
> #11   Fall 0.6722337
> #15   Fall 0.6722337
> #2  Winter 0.9318599
> #6  Winter 0.9318599
> #10 Winter 0.9318599
> #14 Winter 0.9318599
> #4  Spring 0.1927715
> #8  Spring 0.1927715
> #12 Spring 0.1927715
> #16 Spring 0.1927715
>
> Any other thoughts?
>
> JR
>
>
> -----Original Message-----
> From: Bert Gunter [mailto:gunter.berton at gene.com]
> Sent: Thursday, October 11, 2012 10:19 AM
> To: ROLL Josh F
> Cc: r-help at r-project.org
> Subject: Re: [R] Sorting a data frame by specifying a vector
>
> ?order
> df[order(yourcolumn, ]
>
> -- Bert
>
>
> On Thu, Oct 11, 2012 at 10:08 AM, LCOG1 <jroll at lcog.org> wrote:
>> Hello all,
>>    I cannot seem to figure out this seemingly simple procedure.
>>
>> I want to sort a data frame by a specified character vector.
>>
>> So for :
>>
>> df.. <-
>> data.frame(Season=rep(c("Summer","Fall","Winter","Spring"),4),Obs=
>> runif(length(rep(c("Summer","Fall","Winter","Spring"),4))))
>>
>> I want to sort the data frame by the seasons but in the order I
>> specify since alphapetically would not put the season in sequential
>> order
>>
>> I tried the following and a few other things but no dice.  It looks
>> like I will have to convert to factors.  Any thoughts?  Thanks
>>
>> df.. <-
>> df..[sort(as.factor(Df..$Season,levels=c("Summer","Fall","Winter","Spr
>> ing"))),]
>>
>> Josh
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Sorting-a-data-frame-by-specifying-a-vec
>> tor-tp4645867.html Sent from the R help mailing list archive at
>> Nabble.com.
>>
>> ______________________________________________
>> 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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm




More information about the R-help mailing list