Enclosing variables within for loop
问题 So consider the following chunk of code which does not work as most people might expect it to #cartoon example a <- c(3,7,11) f <- list() #manual initialization f[[1]]<-function(x) a[1]+x f[[2]]<-function(x) a[2]+x f[[3]]<-function(x) a[3]+x #desired result for the rest of the examples f[[1]](1) # [1] 4 f[[3]](1) # [1] 12 #attempted automation for(i in 1:3) { f[[i]] <- function(x) a[i]+x } f[[1]](1) # [1] 12 f[[3]](1) # [1] 12 Note that we get 12 both times after we attempt to "automate". The