[Rd] Another code to drop factor levels

From: Suharto Anggono Suharto Anggono <suharto_anggono_at_yahoo.com>
Date: Mon, 05 Nov 2012 08:49:39 -0800 (PST)

I apologize if this is not appropriate for this mailing list.

In R, there is already functionality to drop unused factor levels. However, I am proposing the code below that I wrote. In some occasions, it was faster than applying function 'factor'. In any case, there is no restriction for anyone to use the code below.

droplevels2 <- function(x) {
if (is.null(levels(x)))
 stop("no 'levels' attribute")
nlev <- length(levels(x))
y <- unclass(x)
tb <- tabulate(y, nlev)
used <- as.logical(tb)
tb[used] <-
 seq(length=sum(used))
y[] <- tb[y]
levels(y) <- levels(x)[used]
attr(y, "class") <-
 attr(x, "class")
y
}

Alternatively, one may use 'levels<-.factor' by assigning NA to unused levels, like below.

levels(x)[ tabulate(
unclass(x),
length(levels(x))) == 0 ] <-
NA



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 05 Nov 2012 - 16:51:49 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

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 Wed 07 Nov 2012 - 18:00:50 GMT.

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

list of date sections of archive