s-news
[Top] [All Lists]

Re: [S] logical tests on factors and ordered factors

To: s-news@wubios.wustl.edu
Subject: Re: [S] logical tests on factors and ordered factors
From: Todd.Taylor@pnl.gov (Z. Todd Taylor)
Date: Fri, 24 Apr 1998 07:12:22 -0700
Cc: C.Evans@sghms.ac.uk, zt_taylor@pnl.gov
Sender: owner-s-news@wubios.wustl.edu
Chris Evans <C.Evans@sghms.ac.uk> wrote:

> I want to extract a subset from a dataframe on the values of two
> ordered factors in it.  I have tried things like:
>
> subset <- results[power == 1 & cut.frac == .975]
>
> (those levels of power and cut.frac do exist in results as
> confirmed by summary(results).  However, this is not working.  ...
 
This looks like one of the well-known oddities of factors.  To
do equality tests, you must compare the factor against character   
data: 
 
   subset <- results[power == "1" & cut.frac == ".975"]
 
Or, if you really want to do a numeric comparison:
 
   subset <- results[as.numeric(as.character(power)) == 1 & 
                     as.numeric(as.character(cut.frac)) == .975] 
 
In other words, as.numeric(factor.object) doesn't do what you
think it should.  It instead returns the value of an internal 
integer representation of the factor levels--rarely (if ever) 
what you want.

To illustrate another thing you can't do with factors:

   zoo <- factor(c("dog", "cat", "rat", "cat", "dog"))
   print(paste("The animal is a", zoo))

That has to be

   print(paste("The animal is a", as.character(zoo)))

The general rule is, except for sending it to one of the
modelling functions or trellis, you can almost never use a
factor object for anything without wrapping it in as.character()
or as.numeric(as.character()).  One notable exception is
your example--simple comparison with character data.

--Todd 
--
Z. Todd Taylor
Pacific Northwest National Laboratory
Todd.Taylor@pnl.gov
Why do press and depress both mean "push on it"?
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>