I have a list with vectors of options (my actual list is much longer).
Data <- structure(list(A = c("01", "02", "03", "04", "05", "06",
"07", "08", "09", "99"), B = c("0", "1", "2", "3", "4",
"5", "6", "7", "99"), C = c("00", "10", "11", "12", "13",
"14", "15", "16", "17", "99")), .Names = c("A", "B",
"C"))
I need to iterate over all the combinations of these options.
I was using expand.grid to create a dataframe with the combinations, and then iterating over that dataframe one row at a time.
combinations <- expand.grid(Data, stringsAsFactors = FALSE)
For this small sample 'combinations' results in 900 rows, but it grows exponentially the bigger Data is, and the result for my data is way too large.
I was thinking I could convert my list to a matrix like
Data.df <- data.frame(do.call(rbind, lapply(Data, "length<-", max(vapply(Data, length, 1L)))))
and then use an 'index' column that would correspond to the current iteration for each row. They would all start initialized to 1, and I could use them to get the current set.
Data.df$index <- 1
Data.df$current <- Data.df[cbind(1:nrow(Data.df), as.numeric(Data.df$index))]
The problem I am having is how to actually iterate over all the options (short of using nested loops for the number of rows I have). Since some of the rows of the dataframe are "NA"s, when it gets to one it would mean that row is "finished" and would have to move the next index.
If there is a different, better way of doing this, I open to suggestions as well...