Deb Deb - 4 months ago 7
R Question

All unique samples from a data set in R

Here's my data

z<- c("COP","CHK","BP","BHI","CVX")


if i do

sample(z,3,replace=FALSE)


This will give me 1 unique random sample of 3 from my data set.

I want to find all possible unique samples of 3 from my data set. In this case there will be 10 outcomes.

But how do I write a R code for it?

Please help

Answer

We can use combn to get the unique combinations

t(combn(z, 3))

If we need to sample it

t(combn(sample(z), 3))