Skip to content
Back to formatted view

Raw Message

Message-ID: <971536df0504152231d2a1f1d@mail.gmail.com>
Date: 2005-04-16T05:31:24Z
From: Gabor Grothendieck
Subject: "chronological" ordering of factor in lm() and plot()
In-Reply-To: <000501c5423c$dd0217a0$863dd080@oemcomputer>

On 4/16/05, Eric C. Jennings <matheric at myuw.net> wrote:
> I am trying to do some basic regression and ANOVA on cycle times (numeric
> vectors) across weekdays (character vector), where I have simply labelled my
> days as:
> days<- c("mon","tue","wed"...etc).
> (NOTE: There are actually multiple instances of each day, and the data is
> read-in from a .dat file.)
> 
> I have no trouble at all with the actual number crunching, It is the
> "proper" ordering of the factor that I am asking about. R first alphabetizes
> it("fri","mon","thu"...) before doing the work of lm(), aov() and especially
> plot().
> 
> I have tried as.ordered(factor( )), but that doesn't do anything.
> If I re-assign levels() in the way that I want, that just renames the the
> levels of the factor but does not reorder it internally.
> I've looked at chron(), but that seems to entail using a numeric vector
> instead of a character vector.
> 
> How can I get it to "properly" (chronologically) order the factor. (In some
> ways I'm thinking that all I can do is:
> days<- c("a.mon","b.tues","c.wed"...etc)
> 

Try this:

> f.wrong <- factor(c("mon", "fri", "mon", "fri"))
> f.wrong
[1] mon fri mon fri
Levels: fri mon

> f.right <- factor(f.wrong, levels = c("mon", "fri"))
> f.right
[1] mon fri mon fri
Levels: mon fri