3
votes
$\begingroup$

I am fairly new to R and I have been reading all related topics but still I don't manage to solve my rather simple problem.

I have a dataset with 5 cases for two variables (X,Y), for instance:

  X |  Y
--------
100 | 12
112 | 14
140 | 21    
190 | 20
 89 |  7

Now I want to replicate this dataset with all possible unique combinations of both variables. If I'm not mistaken there would be !5 = 120 different sets.

Now, I have been trying to do this with the 'sample' command. By keeping E constant and then sampling F, I can get a new dataset with different combinations of E and F. The problem of course is that this doesn't return all possible combinations. If I would do this 120 times, I would not get unique datasets.

Thanks in advance for your help.

Best regards, P

$\endgroup$
3
votes
$\begingroup$

permn from the package combinat will give you all the permutations of a vector. Then a simple loop will get you all your datasets:

x <- c(100, 112, 140, 190, 89)
y <- c(12, 14, 21, 20, 7)
z <- permn(x)
for(i in 1:length(z)){
  z[[i]] <- rbind(z[[i]], y)
}
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.