[R] Variables captured in closures get copied?

Titus von der Malsburg malsburg at gmail.com
Wed Feb 11 18:09:05 CET 2009

Hi list!  I have a data frame called fix and a list of index vectors
called rois:

  > head(rois, 3)
  [1] 2 1

  [1] 3

  [1]  6  7 28 26 27 24 25

The part that's causing the issue is the following line:

  lapply(rois, function(roi) fix$x[roi] <- 100)

So for every index vector I'd like to set the respective entries in the
data frame (fix) to 100.

I expected the data frame would be changed after lapply but instead it
remains unchanged.  I understand that when I pass an argument into a
function it gets passed as a value and not as a reference.  But here fix
is not an argument but captured in the closure.  Do my questions are:
What's going on here and what is the idiomatic way of achieving my goal?

Thanks for any help!


More information about the R-help mailing list