Why does For Loop return the same function

Given the following function which returns a function

get_function <- \(model) {
  meanlog <- model[['Mean']]
  sdlog <- model[['SD']]
  \(x){ dlnorm(x, 
               meanlog = meanlog, 
               sdlog = sdlog)
  }
}

When I use the get_function as follow:

func <- distributions[['866064']] |> get_function()

func return the correct function. All work as expected, Great!

However, when I try to use the get_function in a for loop, like so:

output <- NULL
for(product in names(distributions)) {
  output[[product]] <- distributions[[product]] |> get_function()
}

All the functions loaded in the output list are the same, where they should be different because the model associated with each product in the distributions list is different.

What am I doing wrong? How can I get the correct function loaded into the output list based on the different models in the distributions list?



from Recent Questions - Stack Overflow https://ift.tt/3l076Wy
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)