[R] Writing a Permutation Function
R. Michael Weylandt
michael.weylandt at gmail.com
Sat Apr 28 21:11:00 CEST 2012
It's _highly_ inefficient to grow the "holder" each step of the loop
-- you'll see massive speedups by setting something like
holder = character(4000) # Make an empty character vector of length 4000
# inside the loop just fill the part you're looking at
holder[i:(i+3)] = perm
Michael
On Sat, Apr 28, 2012 at 11:23 AM, petermec <petermec at buffalo.edu> wrote:
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list