[R] force apply() to return a list

Robin Hankin r.hankin at noc.soton.ac.uk
Mon Nov 21 10:00:42 CET 2005


Hi again Dimitris



On 21 Nov 2005, at 08:52, Dimitris Rizopoulos wrote:

> I've also changed the f() function, i.e.,

f <- function(x){list(1:max(x[1], 4))}
lapply(apply(cbind(1:5,5:1), 1, f), "[[", 1)
>
>


oops, I missed that.  Works fine now, with both cases, and I
can use this.

Thanks!

Still, it's a little disconcerting that apply() as generally used
can return a list 99.9% of the time and a matrix the other 0.1%.
It took me a long time to track this down when debugging my
code the other night.

Does anyone else agree?  Would it be possible to add an
argument such as force.list (with default FALSE) to apply()
that makes apply() return a list under all circumstances?

Also, adding Dimitris's excellent workaround to apply()'s manpage
would be helpful.

best wishes

rksh


> I hope it'll work now.
>
> Best,
> Dimitris
>
>

--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743




More information about the R-help mailing list