Re: [S] tapply seems to ignore simplify: UNDOCUMENTED list-arrays and abind()

Jens Oehlschlaegel (oehl@Psyres-Stuttgart.DE)
Fri, 3 Jul 1998 20:14:29 +0200 (MET DST)


The returned object in your example is really a strange one:

Quoting from the help file (WinS+3.3): "Another way of saying this is
that the result is a list that has a dim attribute (this
prints as a list, but you can subscript it like an array)."

BTW, the help file said "prints as a list", which is not the full truth
and cause not-seeing-it-as-a-list: it prints like a list which can
be subscripted like an array, or more technically, like an array of mode
list.

This data structure was puzzeling for me. I did not know that this is a
legal type of object in S+. Ready to blame my bad memory I re-inspected
help files at "array", "list" and "Subscript": no reference to this
issue.

Is this an undocumented language feature?

playing with this feature give some strange results:

# taking a matrix
m <- matrix(1:9, 3)
is.list(m)

#### now creating the strange object ###
l <- m
mode(l) <- "list"
l
# l looks like a matrix, but is a list
is.list(l)
# which can be seen by extracting an element
l[1,1]

#### now try to reconvert it ####
n <- as.matrix(l)
n
# failed: n looks like a matrix, but is a list
is.list(n)
# which can be seen by extracting an element
n[1,1]

# this also fails
as.numeric(l)

# well we changed the mode: rechanging also fails
mode(l) <- "numeric"

# giving up on this and do something silly:
# coercing a list to a list changes the list
as.list(l)

#### Try some functions ####

# transpose works
t(m)
# but math not
m*2

#### What about functions aiming on arrays? ####

# apply seems to work
apply(n, 1, FUN=function(le){sum(unlist(le))})

# aperm seems to work
aperm(n, c(2,1))

# a useful userdefined function for arrays doesn't work:
# abind() binds 'list-array' and the result is a numeric array
n <- abind(l,l, along=3)
# results in a standard array
is.list(n)
n[1,1,1]
# the contributor is not to be blamed: how should he have known?

Best regards

Jens Oehlschlaegel

On 2 Jul 1998, Douglas Bates wrote:

> Sorry to follow up on my own post but I was wrong in my example. I
> thought that the result was not a list because it didn't look like a
> list when printed. Samuel E. Buttrey <buttrey@sun10or.or.nps.navy.mil>
> pointed out to me that if you save the result and examine it, it
> really is a list.
>
> S> foo <- tapply( ttt, list(factor(col(ttt), labels = c("bar", "baz", "boz"))),
> + function(x) 32, simplify = FALSE )
> S> mode(foo)
> [1] "list"
> S> length(foo)
> [1] 3
> S> names(foo)
> [1] "bar" "baz" "boz"
>
> Douglas Bates <bates@cs.wisc.edu> writes (incorrectly as it turns out):
>
> > 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
> -----------------------------------------------------------------------
> 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
>

--
Jens Oehlschlaegel-Akiyoshi
Psychologist/Statistician

CURRENT ADDRESS (until 12/98) | PERMANENT VIRTUAL ADDRESS ------------------------------------------------------------------------ | http://www.geocities.com/Tokyo/Bay/5663/ oehl@psyres-stuttgart.de | oehl@geocities.com +49 711 6781-408 (phone) | +49 711 6876902 (fax) |

Center for Psychotherapy Research | Christian-Belser-Strasse 79a | D-70597 Stuttgart Germany |

(general disclaimer)

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