I would like to find all combinations of vector elements that match a specific condition. The expand.grid function returns all possible combinations without checking for a specific state. You can verify a specific condition after using the expand.grid function, but in some situations the number of possible combinations is too large to generate them using expand.grid. Therefore, there is a function that allows me to check the condition when creating all possible combinations.
This is a simplified version of the problem:
A <- seq.int(12, from=0, by=1)*15 B <- seq.int(27, from=0, by=1)*23 C <- seq.int(18, from=0, by=1)*18 D <- seq.int(33, from=0, by=1)*10 out<-expand.grid(A,B,C,D) #out is a dataframe with 235144 x 4 as dimensions idx<-which(rowSums(out)<=400 & rowSums(out)>=300) #Only a small fraction of 'out' is needed results <- out(idx,)
source share