[19037] in s-news-athena
Re: [S] Summary: Adding levels to a factor
daemon@ATHENA.MIT.EDU (Prof Brian D Ripley)
Tue Aug 24 17:35:37 1999
Date: Tue, 24 Aug 1999 22:27:29 +0100 (BST)
From: Prof Brian D Ripley <ripley@stats.ox.ac.uk>
To: "Z. Todd Taylor" <Todd.Taylor@pnl.gov>
Cc: s-news@wubios.wustl.edu
In-Reply-To: <199908241819.LAA00904@aggie.pnl.gov>
Message-Id: <Pine.GSO.4.05.9908242215520.5328-100000@auk.stats>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Hmmmmmmmmmmmmmmmmmmmmmmmmmmmmm, I think.
On Tue, 24 Aug 1999, Z. Todd Taylor wrote:
>
> Prof Brian D Ripley <ripley@stats.ox.ac.uk> wrote:
>
> >
> > Rather than re-invent the wheel, one could note that na.tree.replace
> > adjust all the factors in a data frame to have level "NA" if needed.
> > Its code does not convert to character and back. However, you could
> > just use it!
> >
>
> Hmm. na.tree.replace seems to expect a data frame, not an
Yes, I said so, in line 2.
> individual factor. Are you suggesting to modify that or pull
> out the relevant parts?
Either. I actually sent Jean the relevant parts, and I did say it
worked on all columns of a data frame (which is what he wanted).
But
aa <- somefactor
aa1 <- nas.tree.replace(as.data.frame(aa))$aa
is easy to use.
> Looking inside, the important
> expression appears to be:
> x <- factor(x, c(1:4, "NA"), c(levels(xx), "NA"), exclude = NULL)
>
> Beware that 1:4 hard-coded limit.
Where is that from? Not na.tree.replace, but an irrelevant section of
na.tree.replace.all, I bet. My copy (3.4) has
pos <- is.na(x)
cl <- class(x)
class(x) <- NULL
lev <- c(attr(x, "levels"), "NA")
x[pos] <- length(lev)
levels(x) <- lev
class(x) <- cl
> Also, na.tree.replace seems to have been replaced by
> na.tree.replace.all in Splus 5.*.
Nope, that is old and deprecated, and na.tree.replace _is_ in my copy of
5.1, with very similar code.
Try reading the help some time: ?na.tree.replace.all might illuminate you.
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-----------------------------------------------------------------------
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