7

I have a vector with 663 elements. I would like to create random samples from the vector equal to the length of the vector (i.e. 663). Said differently, I would like to take random samples from all possible orderings of the 663 elements. My goal is to create a data frame of the N random samples or randomly selected permutations.

I have tried the following:

library(combinat)
perms <- as.data.frame(permn(1:663))

Since there are so many possible permutations, I would receive an error message.

My next idea would be to create a data frame with as many rows as I would like samples/permutations and as many variables as elements (i.e. 663) and use a function like sapply() with sample(). However, I don't think this approach is that efficient.

I have also tried:

samples <- replicate(100, table(sample(1:663, 663,replace = F))) 

but I just get a data frame with 100 columns of ones.

David C.
  • 1,974
  • 2
  • 19
  • 29
RTrain3k
  • 845
  • 1
  • 13
  • 27
  • 1
    Because you are taking the `table`. The frequency is just 1 as you are using `replace=FALSE` – akrun Feb 05 '17 at 03:37

1 Answers1

6

replicate will work

a <- 1:663 #vector of 663 elements
perms <- as.data.frame(replicate(100, sample(a)))
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
radek
  • 76
  • 2