[Rd] (PR#9589) 'union' does not handle factors while 'intersect'

ripley at stats.ox.ac.uk ripley at stats.ox.ac.uk
Thu Mar 29 13:19:34 CEST 2007


This is not a bug, nor is the subject line true: both do accept 
factors but what they do with factors is undocumented.  From the help 
(not 'man') page

      Performs *set* union, intersection, (asymmetric!) difference,
      equality and membership on two vectors.
                                     ^^^^^^^
so it is not said to work on factors.  I disagree that what intersect() 
for factors does is 'sensible': e.g. it is asymmetric in its arguments.

The 'standard' behaviour would be for R to coerce arguments to whatever 
they are documented to be, here using as.vector, and we will consider 
doing that.


On Thu, 29 Mar 2007, prechelt at inf.fu-berlin.de wrote:

> Full_Name: Lutz Prechelt
> Version: 2.4.1
> OS: Windows XP
> Submission from: (NULL) (160.45.111.67)
>
>
> 'union' ignores the fact if its arguments are factors while all other set
> operations (intersect, setdiff, setequal, is.element) treat them sensibly.
> The manpage does not even mention the term 'factor'.

Indeed, it excludes them as valid inputs.

> Example:
> f1=factor(c("a","b","b","c"));
> f2=factor(c("a","d","d","e"));
> print(union(f1,f2));
> print(intersect(f1,f2));
> print(setdiff(f1,f2));
> print(setequal(f1,f2));
> print(is.element(f1,f2))
>
> prints
>
> [1] 1 2 3
> [1] a
> Levels: a d e
> [1] b c
> Levels: a b c
> [1] FALSE
> [1]  TRUE FALSE FALSE FALSE
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
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



More information about the R-devel mailing list