这可以满足您的需要。它将采用每个变量的排列而不是整体。这在技术上是相同的,但我相信它会加快速度。
a <- 1:100
b <- 1:100
c <- 1:100
yourdatamatrix <- cbind(a, b, c)
现在我们有了一些数据,函数来了:
PermutationFunction <- function (data, k) {
# creating matrix: amount of variables * amount of permutations
permutations <- matrix(1:(k * length(data[1,])), nrow=k)
row <- NULL
# Output will have as many columns as there are variables.
for (i in 1:length(data[1,])) {
permutations[ ,i] <- sample(data[ , i], k, replace=FALSE)
}
permutations
}
PermutationFunction(yourdatamatrix, k = 10)
时间检查(40 个变量,每个变量有 10000 个值,取 5000):
system.time(PermutationFunction(yourdatamatrix, 5000))
> system.time(PermutationFunction(yourdatamatrix, 5000))
user system elapsed
0.05 0.00 0.05