# Re: [R] How to recode a factor level (within the list)?

From: Lauri Nikkinen <lauri.nikkinen_at_iki.fi>
Date: Sun, 2 Dec 2007 21:06:30 +0200

Thank you Prof Ripley for your solutions, I'll get by with these.

Lauri

2007/12/2, Prof Brian Ripley <ripley_at_stats.ox.ac.uk>:
> On Sun, 2 Dec 2007, Lauri Nikkinen wrote:
>
> > #Dear R-users,
> > #I have a data.frame like this:
> >
> > y1 <- rnorm(10) + 6.8
> > y2 <- rnorm(10) + (1:10*1.7 + 1)
> > y3 <- rnorm(10) + (1:10*6.7 + 3.7)
> > y <- c(y1,y2,y3)
> > x <- rep(1:3,10)
> > f <- gl(2,15, labels=paste("lev", 1:2, sep=""))
> > g <- seq(as.Date("2000/1/1"), by="day", length=30)
> > DF <- data.frame(x=x,y=y, f=f, g=g)
> > DF\$g[DF\$x == 1] <- NA
> > DF\$x[3:6] <- NA
> > DF\$wdays <- weekdays(DF\$g)
> >
> > DF
> >
> > #Frequences
> > g <- lapply(DF, function(x) as.data.frame(table(format(x))))
> > g
> >
> > #NA:s are now part of factor levels. How to recode NA:s into e.g. "missing"?
>
> Not so:
>
> > sapply(DF, class)

> x y f g wdays
> "integer" "numeric" "factor" "Date" "character"
>
> and DF\$f does not have any NA levels.
>
> The place you may think you have got NAs is in format(wdays):
> they are not NA nor "NA" but "NA ". I am not sure what exactly you
> want (NA is not appearing in the tables: see the 'exclude' argument),
> but perhaps
>
> lapply(DF, function(x) {
> if(is.character(x)) x[is.na(x)] <- "missing"
> as.data.frame(table(format(x)))
> })
>
> or
>
> lapply(DF, function(x) {
> z <- table(format(x))
> names(z)[grep("^NA", names(z))] <- "missing"
> as.data.frame(z)
> })
>
> or
>
> lapply(DF, function(x) {
> z <- table(x, exclude=character(0))
> names(z)[is.na(names(z))] <- "missing"
> as.data.frame(z)
> })
>
> ?
>
> --
> Brian D. Ripley, ripley_at_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 272866 (PA)
> Oxford OX1 3TG, UK Fax: +44 1865 272595
>

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. Received on Sun 02 Dec 2007 - 19:14:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 02 Dec 2007 - 19:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.