I am trying to create a loop where i can get all possible combinations in one data frame. The reason for this is i want to use those pairs later to create lm() and adf.test() later on. As an example i have a data frame as follows:
df <- as.data.frame(cbind(1, 2, 3, 4)).
From this i want to get all possible combinations as such:
pairs <- as.data.frame(cbind(c(1, 1, 1, 2, 2, 3), c(2,3,4,3,4,4))).
To accomplish this i have tried several combinations of an for loop similar to this:
all_pairs = matrix(0, ((length(df)*(length(df)-1))/2), 2)
for (ij in 1:((length(df)*(length(df)-1))/2)) {
for (i in 1:(length(df)-1)) {
for (j in (i+1):length(df)) {
all_pairs[ij, 1] = df[i,]
all_pairs[ij,2] = df[j,]
}
}
}
The reason for ((length(df)*(length(df)-1))/2), is then that comb=n(n-1)/2 is how i would calculate all combinations without replacement.
As earlier mentioned, i have tried a few ways of doing this, but none of them works. Is this a good method for accomplishing my goal? if yes, how could i make it work?
Thanks in advance!