[19034] in s-news-athena

home help back first fref pref prev next nref lref last post

=?iso-8859-1?Q?R=E9f._:_[S]_Summary:_Adding_levels_to_a_factor?=

daemon@ATHENA.MIT.EDU (=?iso-8859-1?Q?"G=E9rald_Jean"?=)
Tue Aug 24 14:31:59 1999

From: =?iso-8859-1?Q?"G=E9rald_Jean"?= <Gerald.Jean@spgdag.ca>
To: s-news@wubios.wustl.edu
Message-Id: <852567D7.006430E5.00@mail.spgdag.com>
Date: Tue, 24 Aug 1999 14:30:39 -0400
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: 8bit


Thanks to prof. Ripley,

of course his solution works and all the factors can be done at once.

> ttt.new_na.tree.replace(ttt.po2[,index.of.factor.variables])

Thanks again

Gérald Jean
Analyste-conseil (statistiques), Actuariat
télephone            : (418) 835-8839
télecopieur          : (418) 835-5865
courrier électronique: gerald.jean@spgdag.ca

"In God we trust all others must bring data"





"Gérald Jean" <Gerald.Jean@spgdag.ca> le 99/08/24 13:49:09
                                                           
                                                           
                                                           
 Pour  s-news@wubios.wustl.edu                             
 :                                                         
                                                           
 cc :  (ccc : Gérald Jean/Assurances/SPGDAG/Desjardins)    
                                                           
                                                           
                                                           
 Objet [S] Summary: Adding levels to a factor              
 :                                                         
                                                           







Thanks to all who responded (Todd Taylor, Keenan Sean, Samuel Buttrey,
Leonid Gibiansky, S.D. Byers, Bill Dunlap, Don McKenzie and Yuelin Li)  the
original message follows.   All suggested a similar approach, i.e. first
convert the factor to character and then convert back to factor adding the
new level in the process; one thing I forgot to say is that I was not
creating the factor variables myself, there is several with the same
problem, they were created by someone else in a SAS program and I imported
the data set using Frank Harrel's "sas.get" function.  Here is an exemple
solution from Todd Taylor:

 new <- factor(as.character(old),
                levels=c(levels(old), ""),
                labels=c(levels(old), "Missing")
               )

Again thanks to all,

Gérald Jean
Analyste-conseil (statistiques), Actuariat
télephone            : (418) 835-8839
télecopieur          : (418) 835-5865
courrier électronique: gerald.jean@spgdag.ca

"In God we trust all others must bring data"

The original message:

Hi there,

Suppose I have a factor variable with 2 levels.

> levels(ttt.dcontra)
[1] "N" "O"

But in fact there is a whole lot of missing values in that variable.

> summary(ttt.dcontra)
    N    O NA's
 5639 7468 4593

The missing values mean something to me and I'd like to assign them some
factor level,
"Missing" say, that S+ will treat as a valid level in further analysis, in
regression
for exemple.  How do I do that?

If I try to assign a value to the "NA's", S+ complains.

> ttt.dcontra[which.na(ttt.dcontra)] _ 'Missing'
Warning messages:
  replacement values not all in levels(x): NA's generated in:
"[<-.factor"(ttt.d
contra, which.na(ttt.dcontra), value = "Missing")

If I try to assign a new level to the variable, S+ complains again.

> levels(ttt.dcontra) _ edit(levels(ttt.dcontra))
Error in "levels<-.factor"(ttt.dcontra, edit(level..: number of levels
differs:
2 old, 4

I don't have anymore ideas, any help would be appreciated, I'll summarize
to the list.



-----------------------------------------------------------------------
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



-----------------------------------------------------------------------
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

home help back first fref pref prev next nref lref last post