Skip to content

Behavior of apply()

3 messages · Doran, Harold, Marc Schwartz, R. Michael Weylandt

#
On Dec 8, 2011, at 3:30 PM, Doran, Harold wrote:

            
Harold,

Per the Value section of ?apply:

If the calls to FUN return vectors of different lengths, apply returns a list of length prod(dim(X)[MARGIN]) with dim set to MARGIN if this has length greater than one.


In your first example, the first row only has one value that is returned by signif() and then second row has two. So a list is returned.

In your second example, both rows have two values that are returned by signif(), thus a matrix can be returned.

The key is not that the two objects are both matrices, but that there are differing lengths in the returned values from the rows in each.

HTH,

Marc Schwartz
#
It's because your first example produces answers of varying length so
there's no natural way to coerce it to a matrix. Your second has a
consistent length so the result can be made into a matrix.

Michael
On Thu, Dec 8, 2011 at 4:30 PM, Doran, Harold <HDoran at air.org> wrote: