From daniel.kaschek at physik.uni-freiburg.de  Mon Feb 23 21:57:49 2015
From: daniel.kaschek at physik.uni-freiburg.de (Daniel Kaschek)
Date: Mon, 23 Feb 2015 21:57:49 +0100
Subject: [Rd] iterated lapply
Message-ID: <1424725069.2636.5.camel@physik.uni-freiburg.de>

Hi everybody,

with the following code I generate a list of functions. Each function
reflects a "condition". When I evaluate this list of functions by
another lapply/sapply, I get an unexpected result: all values coincide.
However, when I uncomment the print(), it works as expected. Is this a
bug or a feature?

conditions <- 1:4
test <- lapply(conditions, function(mycondition){
  #print(mycondition)
  myfn <- function(i) mycondition*i
  return(myfn)
})

sapply(test, function(myfn) myfn(2))



Cheers,
Daniel

-- 
Daniel Kaschek
Institute of Physics
Freiburg University

Room 210
Phone: +49 761 2038531



[Rd] iterated lapply
Daniel Kaschek