I am looking for the most efficient way to create combinations based on n different series in R.
Base R has this nice function called expand.grid which returns all the combinations as a data frame, but I need each and every combination as vector as a separate list elements.
So from there it should not be - and it is not - a difficult task to achieve what I want, but the fact that first I need to create a data frame seems to be an unneeded step in this process.
Let's have an example: Let's assume I want all the combinations, where the first element is 1,2,3 or 4, the second one is 1 or 2, and the third one is 1,2 or 3. The input should be the highest integer in the series:
library(dplyr)
c(4,2,3) %>% #This is the input; the length can be anything, here it happens to be 3
lapply(\(elem) seq(elem)) %>% #Here we create the sequences: 1,2,3,4 & 1,2 & 1,2,3
expand.grid %>% #A data frame with all the possible combinations
{split(unlist(.),seq(nrow(.)))} #We unlist the whole data frame into one vector, and split it into 1,2,...,nrow(data frame) equally sized vectors, which ultimately become list elements
So, is there a more efficient way to achieve this?
(I just added this in order to remove the down vote, Aku-Ville Lehtimäki)
If looking for efficiency, consider writing the code in C++:
Edit
I believe its possible to only use indexing instead of data copying which will significantly improve the speed. Here is a way to achieve half of the indexing:
Note that this is not the best, but its the best I could think of so far