R-alpha: tapply bug

Kurt Hornik Kurt.Hornik@ci.tuwien.ac.at
Mon, 2 Jun 1997 07:19:11 +0200


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

One solution is to replace

	namelist[[i]] <- levels(index)

by

	namelist[[i]] <- unique(levels(index)[index])

but perhaps there is something smarter ...

-k

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