[R] Suggested mod to interaction()
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Wed Jun 16 16:02:33 CEST 2004
May I suggest a slight modification to interaction()?
It's purely cosmetic, but I found it convenient.
It would have no effect on standard use of the function.
In the code below, the change consists of adding an
argument 'sep' (default = ".", which is the standard
behaviour), and changing the line
else as.vector(outer(l, lvs, paste, sep = "."))
to
else as.vector(outer(l, lvs, paste, sep = sep))
-----------------------------------------------------
interaction<-function (..., drop = FALSE, sep=".")
{
args <- list(...)
narg <- length(args)
if (narg == 1 && is.list(args[[1]])) {
args <- args[[1]]
narg <- length(args)
}
ans <- 0
lvs <- NULL
for (i in narg:1) {
f <- args[[i]]
if (!is.factor(f))
f <- factor(f)
l <- levels(f)
ans <- ans * length(l) + as.integer(f) - 1
lvs <- if (i == narg)
l
else as.vector(outer(l, lvs, paste, sep = sep))
}
ans <- ans + 1
if (drop) {
f <- unique(ans[!is.na(ans)])
ans <- match(ans, f)
lvs <- lvs[f]
}
ans <- as.integer(ans)
levels(ans) <- lvs
class(ans) <- "factor"
ans
}
-----------------------------------------------------
Best wishes to all,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 16-Jun-04 Time: 15:02:33
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list