Random sampling without replacement


I have a data frame with 18 values which are fish flows (quantity of fishes/second). Each value is associated with the name of a trap.
I would like to make a loop in order to make all the possible combinations of trap for 1 trap, 2 traps.. until 6 traps. Only unique combinations !

Ex : with 1 trap, only 18 combinations. with 2 traps, 18x18=324 combinations.. with 6 traps, 18^2 = 3401224 possible combinations...
With the corresponding values of flow for each sampling.

For the moment I have this code :
X = my date frame

sX <- list() # list of the different samples
s <- c() # already sampled samples
for (i in 1:18) {
sX[[i]] <- sample(X[!(X %in% s)], 1, replace=FALSE)
s <- c(s, sX[[i]])

sX <- list() # list of the different samples
s <- c() # already sampled samples
for (i in 1:324) {
sX[[i]] <- sample(X[!(X %in% s)], 2, replace=FALSE)
s <- c(s, sX[[i]])

.. etc until 6 traps.

And it is almost great but it does not replace the traps, while i would like R to not replace the combination of traps. For example, for a combination of 2 traps, i have only 9 combinations because R doesn't make combination with a trap already sampled.

I hope it is clear !
Thank you so much for your help.

Hi, can you provide a reproducible example of your dataset?


My dataset (very simple) looks like this :

the letters are the names of the traps, the values are the flows.

I'm sure you shared this image with the best intentions, but perhaps you didnt realise what it implies.
If someone wished to use example data to test code against, they would type it out from your screenshot...

This is very unlikely to happen, and so it reduces the likelihood you will receive the help you desire.
Therefore please see this guide on how to reprex data. Key to this is use of either datapasta, or dput() to share your data as code

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.