R-alpha: R 0.50.a3 tapply patch

Kurt Hornik Kurt.Hornik@ci.tuwien.ac.at
Mon, 11 Aug 1997 15:36:09 +0200


Can we please apply the following patch?  It fixes the following problem
I already reported:

************************************************************************
There is a problem with tapply() in the case where INDEX is a factor
with at least one of its levels not occurring in it.

In this case,
	namelist[[i]] <- levels(index)
gets all levels but
	group <- group + ngroup * (codes(index) - 1)
only those occurring in index, which subsequently causes
	names(ans) <- namelist[[1]]
to fail.

For example,

R> f <- factor(c("a", "a", "b"), levels = letters[1:3])
R> f
[1] a a b
R> codes(f)
[1] 1 1 2
R> levels(f)
[1] "a" "b" "c"

which gives e.g.

R> tapply(rep(1, length(f)), f, sum)
Error: names attribute must be the same length as the vector
************************************************************************

Thanks,
-k

--- src/library/base/funs/tapply.orig	Fri Feb  7 01:08:31 1997
+++ src/library/base/funs/tapply	Sun Aug 10 21:57:31 1997
@@ -14,7 +14,7 @@
 		index <- as.factor(INDEX[[i]])
 		if (length(index) != nx) 
 			stop("arguments must have same length")
-		namelist[[i]] <- levels(index)
+		namelist[[i]] <- unique(levels(index)[index])
 		extent[[i]] <- nlevels(index)
 		group <- group + ngroup * (codes(index) - 1)
 		ngroup <- ngroup * nlevels(index)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-