Re: [R] Assignments inside lapply

From: Thomas Lumley <>
Date: Thu, 28 Apr 2011 08:50:27 +1200

On Wed, Apr 27, 2011 at 10:12 PM, Kenn Konstabel <> wrote:
> On Wed, Apr 27, 2011 at 12:58 PM, Nick Sabbe <> wrote:
>> No, that does not work.
>> You cannot do assignment within (l)apply.
>> Nor in any other function for that matter.
> Yes that may work if you want to.
> You can do non-local assignment within lapply using <<- (and, for that
> matter, within any other function) but there is no one-word answer to
> the question whether this is a good idea.

"No" is a fairly good one-word answer in the case of lapply().

The main virtue of the *apply family is the simpler mental model resulting from thinking functionally, and you really don't want to mess that up by using functions with side effects. Especially as the order of evaluation in lapply(), and thus the order of the side-effects, isn't specified. Who knows, someday it might run in parallel.


Thomas Lumley
Professor of Biostatistics
University of Auckland

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 27 Apr 2011 - 20:53:58 GMT

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 Wed 27 Apr 2011 - 21:00:33 GMT.

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

list of date sections of archive