2021年3月29日星期一

What if input needs to be a list in a loop: Any option other than `for`?

Imagine we have a foo <- function() that only takes A_Named_List_of_One_data.frame as input. For example: list(A = data.frame(d = 1:2)).

Now, imagine we have A_Named_List_of_Several_data.frames saved in object L. For example:

L = list(A = data.frame(d = 1:2), B = data.frame(d = 3:5)). So to enter L elements, we need to loop them into foo().

My understanding is that in this situation the only looping option is for as follows. Are there any other options?

  output <- list()    for(i in seq_along(L)) { output[[i]] <- foo(L[i]) }    setNames(output, names(L))  

ps. lapply enters the elements of L into foo as a data.frame while foo needs each element of L to be a A_Named_List_of_One_data.frame.

https://stackoverflow.com/questions/66864138/what-if-input-needs-to-be-a-list-in-a-loop-any-option-other-than-for March 30, 2021 at 10:51AM

没有评论:

发表评论