Say I have a list, preds
, that contains 3 different sets of predictions based on unique models.
set.seed(206)
preds <- list(rnorm(1:100), rnorm(1:100), rnorm(1:100))
I am having trouble iterating through the list and creating a new list. I want a list of new vectors that are composed of the corresponding index in the other vectors. So for example, I can get the first element in each vector of the list using sapply()
like this:
sapply(preds, "[[", 1)
Which works well, but I cannot figure out how to iterate through each element in each vector to construct a new list. I want something like this (doesn't work):
sapply(preds, "[[", 1:100)
Using the above seed and preds
list, my desired output would be something like:
first_element <- sapply(preds, "[[", 1)
second_element <- sapply(preds, "[[", 2)
third_element <- sapply(preds, "[[", 3)
desired_result <- rbind(first_element, second_element, third_element)
But for all elements in the list, which in this example is 100.
Please let me know if this is not clear, and I appreciate any help!