[R] how to concatenate factor vectors?

Sam Steingold sds at gnu.org
Thu Oct 18 18:08:34 CEST 2012


> * William Dunlap <jqhaync at gvopb.pbz> [2012-10-18 15:33:38 +0000]:
>
> c() has an unfortunate history.

:-)
ISTR reading in the R manual ~15(?) years ago that the language was in a
flux and one could not expect code written for the current release to
work in the next release.  I was considering R as the graphing back end
at that time, so this note turned me off.
Now it turns out that R has a legacy it cannot shake. :-)

> Or, you can decide to write  a new concatenation function
> and stop using c().
>
> As for EQ vs. EQUALP, don't even think of EQ in R: it doesn't make
> sense there.  identical() is a pretty quick way to check that two
> objects have identical contents.

Good! That's what I was looking for!

concatenate.factors <- function (x, y) {
  stopifnot(identical(levels(x),levels(y)))
  unlist(list(x,y), use.names=FALSE)
}

This seems to do what I need.

I see that
identical(levels(concatenate.factors(a,b)),levels(a))
==> TRUE
DIUC that concatenate.factors does NOT create an intermediate vector and
then re-factor it?

Thank you very much for your insight!

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://jihadwatch.org http://openvotingconsortium.org
http://www.memritv.org http://memri.org http://truepeace.org
Live Lisp and prosper.




More information about the R-help mailing list