Re: [R] Assignments inside lapply

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

On Wed, Apr 27, 2011 at 10:12 PM, Kenn Konstabel <lebatsnok_at_gmail.com> wrote:
> On Wed, Apr 27, 2011 at 12:58 PM, Nick Sabbe <nick.sabbe@ugent.be> 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

-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive