Enclosing variables within for loop

前端 未结 3 1230
终归单人心
终归单人心 2020-12-06 13:09

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 ini         


        
3条回答
  •  甜味超标
    2020-12-06 13:56

    Will this approach work for you?

    ff<-list()
    for(i in 1:3) {
        fillit <- parse(text=paste0('a[',i,']+x' ))
       ff[[i]] <- function(x) ''
       body(ff[[i]])[1]<-fillit
    }
    

    It's sort of a lower-level way to construct a function, but it does work "as you want it to."

提交回复
热议问题