[R] Factor levels.
Christos Hatzis
christos at nuverabio.com
Wed Oct 3 22:19:36 CEST 2007
My suggestion was based on the standard sort order of a factor.
What I did as an example was to create a factor having your specified
levels:
> x <- factor(sample(c("Unit","Achievement","Scholarship"), 10, TRUE))
> x
[1] Achievement Unit Achievement Unit Scholarship Achievement
Unit
[8] Achievement Unit Unit
Levels: Achievement Scholarship Unit
This creates a factor with the default (sorted) order of levels. Then what
I proposed works, since it is based on the standard behavior of factor. Now
if you have to override this behavior, you will have to deal with the
sorting problem, as Peter suggested.
-Christos
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Rolf Turner
> Sent: Wednesday, October 03, 2007 4:12 PM
> To: Peter Dalgaard
> Cc: r-help list
> Subject: Re: [R] Factor levels.
>
>
> On 4/10/2007, at 7:50 AM, Peter Dalgaard wrote:
>
> > Rolf Turner wrote:
> >>> Does it even work? (What if it is the first or the 2nd
> level that is
> >>> absent?
> >>
> >> Yes it works. What's the problem?
> >>
> >> To beat it to death: if the second level of fff is
> absent then
> >> fff will consist entirely of 1's and 3's,
> >> and so c("U","A","S")[fff] will consist entirely of
> U's and S's.
> >> I can then set the levels to be
> >> c("U","A","S") and get what I want.
> > You didn't say that fff was numeric.
>
> All factors are numeric.
> > If fff is a factor, then we have the problem:
> >
> > > attach(read.table(stdin(),header=T))
> > 0: fff
> > 1: Unit
> > 2: Scholarship
> > 3: Scholarship
> > 4: Unit
> > 5:
> > > c("U","A","S")[fff]
> > [1] "A" "U" "U" "A"
>
> My original fff is a factor with levels c
> ("Unit","Achievement","Scholarship"). If you make that
> adjustment, you get the ``right answer''.
> > Actually we have another problem too, namely sort order....
>
> No, there is no sort order problem. See above.
>
> [Given that the original fff has its levels in the
> order specified
> then Unit maps to U,
> Achievement to A, and Scholarship to S.]
>
> cheers,
>
> Rolf
>
> P.S. ***Are*** there any risks/dangers in following Christos
> Hatzis' suggestion of simply doing
>
> levels(fff) <- c("U","A","S") ???
>
> ######################################################################
> Attention:\ This e-mail message is privileged and
> confidenti...{{dropped}}
>
> ______________________________________________
> 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