s-news
[Top] [All Lists]

RE Removing levels from a factor

To: s-news@lists.biostat.wustl.edu
Subject: RE Removing levels from a factor
From: gerald.jean@dgag.ca
Date: Wed, 5 Mar 2008 08:25:47 -0500
In-reply-to: <OFF1008F4C.CE604E7E-ON69257403.001D738D-69257403.002024DF@nt.gov.au>
Hello Mark,

one thing you could do is redefine your variables as follows:

> newdata$Y <- factor(newdata$Y, levels = c("B", "C", "D"), labels = c("B",
"C", "D"))

That should do it,

Gérald Jean
Conseiller senior en statistiques, Actuariat
télephone            : (418) 835-4900 poste (7639)
télecopieur          : (418) 835-6657
courrier électronique: gerald.jean@dgag.ca

"In God we trust, all others must bring data"  W. Edwards Deming


                                                                           
             Mark.Hearnden@nt.                                             
             gov.au                                                        
             Envoyé par :                                                A 
             s-news-owner@list         s-news@lists.biostat.wustl.edu      
             s.biostat.wustl.e                                          cc 
             du                                                            
                                                                     Objet 
                                       [S] Removing levels from a factor   
             2008/03/05 00:53                                              
                                                                           
                                                                           
             Veuillez répondre                                             
                     à                                                     
             s-news@lists.bios                                             
               tat.wustl.edu                                               
                                                                           
                                                                           






Hello

Quick question I hope.

I have a factor variable ("Treatment") in a data frame ("data") and I want
to
extract rows for certain levels of Treatment and ignore the rest.

eg. levels for Treatment are "A", "B", "C", "D" and "E".

> levels(data$Treatment)

 [1] "A" "B" "C" "D" "E"

I only want Treatments "B", "C" and "D", so I select create "newdata" thus;

newdata <- data[(data$Treatment=="B"|data$Treatment=="C"|data$Treatment
=="D"),]

When I look at means for a response variable "Y" I get:

> tapply(Y, Treatment, mean)

 A        B        C        D  E
NA 1460.274 1296.284 786.0946 NA

My question is: How do I get rid of the level names for "A" and "E" from
the
variable "Treatment" in "newdata" (without using the GUI)?


(Using S+ 7.0 for Windows)

Many thanks


Mark Hearnden
Primary Industry Services

Department of Primary Industry, Fisheries and Mines
Northern Territory Government of Australia
GPO Box 3000
DARWIN  NT  0801

Phone: (08) 899 92131
Fax: (08) 899 92010
Mob: 0401 115 857

"Everything should be made as simple as possible,
  but not simpler." Albert Einstein.

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news



Le message ci-dessus, ainsi que les documents l'accompagnant, sont destinés
uniquement aux personnes identifiées et peuvent contenir des informations
privilégiées, confidentielles ou ne pouvant être divulguées. Si vous avez
reçu ce message par erreur, veuillez le détruire.

This communication (and/or the attachments) is intended for named
recipients only and may contain privileged or confidential information
which is not to be disclosed. If you received this communication by mistake
please destroy all copies.



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