Re: [Rd] factor(x, exclude=y) if x is a factor

From: Suharto Anggono Suharto Anggono <suharto_anggono_at_yahoo.com>
Date: Sun, 09 Dec 2012 23:46:13 -0800 (PST)


After searching, I see that https://stat.ethz.ch/pipermail/r-help/2011-April/276274.html has mentioned this issue, perhaps more clearly.

Thanks for pointing out "Arguments" section about 'exclude'. That documents the code

    exclude <- as.vector(exclude, typeof(x))

A note: if x is a factor, factor(x, exclude=y) doesn't always do nothing other than dropping unused levels.

> x <- 2:3
> x

[1] 2 3
> xf <- factor(x, levels=x)
> xf
[1] 2 3
Levels: 2 3
> factor(xf, exclude=2)

[1] <NA> 3
Levels: 3

> x <- c(2:3, "a")
> x

[1] "2" "3" "a"
> xf <- factor(x, levels=x)
> xf
[1] 2 3 a
Levels: 2 3 a
> factor(xf, exclude=2)

[1] <NA> 3 a
Levels: 3 a

> sessionInfo()

R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:

[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

From: Lorenz, David <lorenz_at_usgs.gov>
Subject: Re: [Rd] factor(x, exclude=y) if x is a factor To: "Suharto Anggono Suharto Anggono" <suharto_anggono_at_yahoo.com> Cc: R-devel_at_r-project.org
Date: Thursday, 6 December, 2012, 10:12 PM

Suharto,  I think that the key is to read the definition of exclude in the Arguments section:a vector of values to be excluded when forming the set of levels. This should be of the same type as x, and will be coerced if necessary.  Because the levels already exist for x as a factor, they are not formed or revised, except to drop unused levels in the case where exclude=NULL (or the default value). To drop level a from x use:

factor(as.character(x), exclude="a")  or, on creation: x <- factor(c("a", "b"), exclude="a")
Dave

On Wed, Dec 5, 2012 at 11:39 PM, Suharto Anggono Suharto Anggono <suharto_anggono_at_yahoo.com> wrote:

factor(x, exclude=factor("a", levels=c("a","b")))



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 10 Dec 2012 - 07:52:08 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 Mon 10 Dec 2012 - 09:32:45 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