[R] Writing a Permutation Function

petermec petermec at buffalo.edu
Sat Apr 28 17:23:56 CEST 2012


Thanks for the input everyone!

So far this is the updated code that I have:

alphabet = c("a","b","c","d")
holder = c()
permute = function(alphabet,n){
for (i in 1:1000){
    perm = sample(alphabet, replace=F, size=n)
    holder = rbind(holder, perm, deparse.level=0)
}
data2 = unique(holder)
data3 = data2[order(data2[,1],data2[,2]),]
labels = apply(data3, 1, paste, sep="", collapse="")
print(data.frame(data3, row.names=labels))
}

Although the code seems to be working for me now, I feel that its still
rough and inherently wrong in some of its nature.

It is basically taking 1000 iterations of sample from the character string
input and from that 1000 iterations, only keeping the unique ones. What is
the character vector that is inputted ends up being 10 characters long, then
surely 1000 iterations wouldn't get all the unique values. Changing 1000
iterations to 1000000000 would be inefficient and CPU consuming.

Thanks again everyone.

--
View this message in context: http://r.789695.n4.nabble.com/Writing-a-Permutation-Function-tp4594621p4594905.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list