Apologies if this has already been answered somewhere, but I checked all the pages I could find and can’t find a solution to this specific problem.
I want to use an apply function to select elements from lists nested within a list. The element I want to select from the sub-lists vary based on a arguments contained in a separate list. Here is some example code to illustrate what I am trying to do:
# Set seed for replicable results
set.seed(123)
# Create list of lists populated with randomly generated numbers
list1 <- list()
for (i in 1:10) {
list1[[i]] <- as.list(sample.int(20, 10))
}
# Create second randomly generated list
list2 <- as.list(sample.int(10, 10))
# For loop with uses values from list 2 to call specific elements from sub-lists within list1
for (i in 1:10){
print(list1[[i]][[list2[[i]]]])
}
####################################################################################
[1] 4
[1] 8
[1] 5
[1] 8
[1] 15
[1] 17
[1] 12
[1] 15
[1] 3
[1] 15
As you can see, I can use a for loop to successfully to select elements from the sub-lists nested within list1
using values from list2
in combination with the iterating value i
.
Solutions offered to questions like this (R apply function with multiple parameters), suggest that I should be able to achieve this same result using the mapply function. However, when I try to do this I get the following error:
# Attempt to replicate output using mapply
mapply(function(x,y,z) x <- x[[z]][[y[[z]]]], x=list1, y=list2, z=1:10 )
####################################################################################
Error in x[[z]][[y[[z]]]] : subscript out of bounds
My questions are:
How can my code can be altered to achieve the desired outcome?
What is causing this error? I have had similar problems with mapply in the past, when I have tried to input one or more lists alongside a vector, and have never been able to work out why it sometimes fails.
Many thanks in advance!