# [S] tapply seems to ignore simplify = FALSE

Douglas Bates (bates@stat.wisc.edu)
02 Jul 1998 15:28:50 -0500

Either I am misreading the documentation or tapply does not correctly
interpret the argument "simplify". I would have expected the result
below to be a list, not a numeric vector. Is this a known
"infelicity"? Does it occur on Windows versions of S-PLUS as well?

S-PLUS : Copyright (c) 1988, 1996 MathSoft, Inc.
S : Copyright AT&T.
Version 3.4 Release 1 for Sun SPARC, SunOS 5.3 : 1996
Working data will be in .Data
S> ttt <- array(TRUE, c(8,3))
S> tapply( ttt, list(factor(col(ttt), labels = c("bar", "baz", "boz"))),
+ function(x) 32, simplify = FALSE )
bar baz boz
32 32 32

The example looks silly but it is close to something I really need to
do in such a way as to produce a list. I have a matrix of logical
values and I need the row positions of the TRUE elements for each column.
The code did look like
lastRow <- apply(isLast, 2, function(x) seq(along = x)[x])
and I "knew" the result would always be a list because the number of
TRUE's in each column was never be exactly equal. Well there is a
boundary case in which there is exactly one TRUE in each column so the
lengths of the returned values from the FUN argument are equal so
apply simplifies the result. I don't know how to prevent apply from
simplifying the result. What I was trying to do above was to replace
the call to apply by a call to tapply which claims to accept a
"simplify" argument.

By the way, in R this does give the result I was anticipating.
R> tapply( ttt, list(factor(col(ttt), labels = c("bar", "baz", "boz"))),
+ function(x) 32, simplify = FALSE )
\$bar
[1] 32

\$baz
[1] 32

\$boz
[1] 32

Turns out there is a much simpler way of accomplishing what I want to
do. I just use
lastRow <- as.list(apply(isLast, 2, function(x) seq(along = x)[x]))
_except_ that S-PLUS then drops the names.

S> c(bar = 1, baz = 2, boz = 3)
bar baz boz
1 2 3
S> as.list(c(bar = 1, baz = 2, boz = 3))
[[1]]:
[1] 1

[[2]]:
[1] 2

[[3]]:
[1] 3

Again, I just offer for comparison that the result in R is what I
would have expected from S-PLUS.
R> as.list(c(bar=1, baz = 2, boz = 3))
\$bar
[1] 1

\$baz
[1] 2

\$boz
[1] 3
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news