how to pass through parameter in apply in R

245 Views Asked by At

ant_list is a list, data is a parameter, how can I pass through the parameter to function in apply, that is:

result<-lapply(ant_list,FUN,...)

how to use the parameter data in the FUN, for example,

ant_list<-list(c(1,2,3,4),
               c(2,3,4,5),
               c(3,4,5,6))
data<-3

result<-lapply(ant_list,function(x){
  result_temp<-x+data
  return(result_temp)
})
1

There are 1 best solutions below

0
On

It is a way to pass through the parameter as followed

ant_list<-list(c(1,2,3,4),
               c(2,3,4,5),
               c(3,4,5,6))
data<-3

result<-lapply(ant_list,function(x,data){
  result_temp<-x+data
  return(result_temp)
},data)