Given the vector:
a <- c(1,2,3)
I'm trying to calculate all vectors containing combinations from the elements in a, which is:
list(
a[c(1,2,3)],
a[c(1,3,2)],
a[c(2,1,3)],
a[c(2,3,1)],
a[c(3,1,2)],
a[c(3,2,1)])
This can be reproduced with:
df <- expand.grid(rep(list(a), length(a)))
nunique <- apply(df, 1, function(x) length(unique(x)))
df <- df[nunique == ncol(df), ]
as.list(as.data.frame(t(df)))
I tried doing this with the expand.grid
but this function gives permutations where elements can be repeated which results in extra large dataset and gives the error from below.
I have seen similar questions to this but fail to find a fast solution that doesn't produce error:
Error: cannot allocate vector of size 37.3 Gb
The error can be reproduced for:
a <- c(1,2,3,4,5,6,7,8,9,10)