Suharto Anggono Suharto Anggono
2012-Nov-05 16:49 UTC
[Rd] Another code to drop factor levels
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
Suharto Anggono Suharto Anggono
2012-Nov-07 16:24 UTC
[Rd] Another code to drop factor levels
This is a variant that does not use 'tabulate'. As before, there is no
restriction for anyone to use this code.
droplevels2i <- function(x)
{
if (is.null(levels(x)))
stop("no 'levels' attribute")
nlev <- length(levels(x))
y <- unclass(x)
used <- logical(nlev)
used[] <- FALSE
used[y] <- TRUE
tb <- used
tb[used] <-
seq(length=sum(used))
y[] <- tb[y]
levels(y) <- levels(x)[used]
attr(y, "class") <-
attr(x, "class")
y
}
------------------------------
Pada Sen, 5 Nov 2012 23:49 ICT Suharto Anggono Suharto Anggono menulis:
>
>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