An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090424/6647fa32/attachment-0001.pl>
argument 'exclude' in function xtabs
3 messages · Erik Iverson, Matthieu Lesnoff
I was willing to use argument 'exclude' in function xtabs to remove some levels of factors (xtabs help page says '"exclude: a vector of values to be excluded when forming the set of levels of the classifying factors").
I think I see what's happening, and it's a little confusing to me, too.
If your classifying factor to xtabs is not
actually a factor, but say, a character vector, it will be converted to a
factor
while using the "exclude" argument. However, if it already is a factor,
this does not happen.
So, for example, contrast:
trt.char <- c("A","B","C","A","B","C")
xtabs(~trt.char, exclude = "B")
with
trt.fac <- factor(c("A","B","C","A","B","C"))
xtabs(~trt.fac, exclude = "B")
Hope that helps,
Erik Iverson
1 day later
Ok thanks Erik Indeed, "exclude" in "xtabs" seems working whith character vectors:
x <- c(rep(c("A","B","C"), 2))
x
[1] "A" "B" "C" "A" "B" "C"
xtabs(~ x, exclude = "B")
x A C 2 2 and not directly with factors:
x <- factor(rep(c("A","B","C"), 2))
x
[1] A B C A B C Levels: A B C
levels(x)
[1] "A" "B" "C"
xtabs(~ x, exclude = "B")
x A B C 2 2 2 while function "table" does it:
x <- factor(rep(c("A","B","C"), 2))
x
[1] A B C A B C Levels: A B C
table(x, exclude = "B")
x A C 2 2 However, one point remains confusing for me in function "table". When I add a NA in the vector, it still works:
x <- factor(c(rep(c("A","B","C"), 2), NA))
x
[1] A B C A B C <NA> Levels: A B C
table(x, exclude = "B")
x A C 2 2 but not anymore when I use argument "exclude = NULL" in function "factor":
x <- factor(c(rep(c("A","B","C"), 2), NA), exclude = NULL)
x
[1] A B C A B C <NA> Levels: A B C <NA>
table(x, exclude = "B")
x A B C <NA> 2 2 2 1 Finally, if I remove NA, it works again:
x <- factor(rep(c("A","B","C"), 2), exclude = NULL)
x
[1] A B C A B C Levels: A B C
table(x, exclude = "B")
x A C 2 2 it looks like "exclude" does not work when NA is a level Regards Matthieu
-----Message d'origine----- De : Erik Iverson [mailto:iverson at biostat.wisc.edu] Envoy? : 24 April 2009 19:13 ? : Matthieu Lesnoff Cc : r-help at r-project.org Objet : Re: [R] argument 'exclude' in function xtabs
I was willing to use argument 'exclude' in function xtabs to remove some levels of factors (xtabs help page says '"exclude: a vector of values to be excluded when forming the set of levels of the
classifying factors").
I think I see what's happening, and it's a little confusing
to me, too.
If your classifying factor to xtabs is not actually a factor,
but say, a character vector, it will be converted to a factor
while using the "exclude" argument. However, if it already
is a factor, this does not happen.
So, for example, contrast:
trt.char <- c("A","B","C","A","B","C")
xtabs(~trt.char, exclude = "B")
with
trt.fac <- factor(c("A","B","C","A","B","C"))
xtabs(~trt.fac, exclude = "B")
Hope that helps,
Erik Iverson