Re: [Rd] Controlling simplification in apply()?

From: Prof Brian Ripley <>
Date: Tue, 21 Feb 2012 07:09:18 +0000

On 20/02/2012 16:03, Gavin Simpson wrote:
> Dear list,
> Fully according to the documented behaviour, I was bitten by apply()
> simplifying results to vectors or arrays when the lengths of the
> individual objects returns by FUN are the same (over simplification).
> sapply() has argument simplify with default TRUE to control whether it
> attempts to simplify the returned objects or not.
> I do find it a little annoying that one is not able to force apply into
> one behaviour or the other.

I think it is different. sapply (and mapply) work on a list and return a list result unless simplified. apply() works on an array and returns an array result (dropping dims to a vector) unless that is not possible.

We don't have an example, but you seem to want a list result from apply(). If that _is_ what you want, why not use lapply()? E.g.

lapply(split(m, row(m)), max)

is the list equivalent of apply(m, 1, max) . Also, apply() is just sugar around a for() loop, and a loop may do what you want.

> Would the be merit in apply() also having a similar argument to control
> whether simplification occurs or not, say adding `simplify = TRUE` after
> `...`? If there was I'd be happy to provide a first stab and contribute
> updated documentation.
> All the best,
> Gavin

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
Received on Tue 21 Feb 2012 - 07:11:52 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 21 Feb 2012 - 13:20:19 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive