Dummy example:
1> L = vector('list',100)#prefill empty list
1> for (i in 1:100){
1+ L[[i]]$letter = letters[rgeom(1,.5)+1]#populate it with a letter
1+ L[[i]]$number = runif(1)#and a number
1+ }
1> i = ceiling(runif(100,0,100))#an (arbitrary) vector of indices
1> x = L[[i]]$letter #I want the ith letter
Error in L[[i]] : no such index at level 2
I want x
to contain the letter
object of the i
th element of L
. (the order of i
doesn't have anything to do with the index order of x
)
What is a good way to do this without a loop?
Here is copy/paste from my editor, instead of my terminal window, for those who may find it easier:
L = vector('list',100)
for (i in 1:100){
L[[i]]$letter = letters[rgeom(1,.5)+1]
L[[i]]$number = runif(1)
}
i = ceiling(runif(100,0,100))
x = L[[i]]$letter