I have a problem where I have a number of vectors (how many vectors unknown in advance) each of which has a different number of elements (also unknown in advance). I want to walk though all the combinations of values, doing an operation for each combination. (The vectors can be lists if that's more convenient.)
If I knew there were three vectors I could write nested for loops, something like this:
for (i in seq_along(x))
for (j in seq_along(y))
for (k in seq_along(z))
# do something more interesting than the next line
cat(x[i],y[j],z[k])
But I don't know how many vectors there will be.
I suspect there is an obvious solution to this. I just don't see it. Any suggestions?
Depending on what your actual calculation is, you might find expand.grid to be a useful function. You can pass it either the individual vectors or a list containing the vectors.