This is my first help post, hope it works!

Just check out the "sample" function
At the command line type:
?sample

I think it will be pretty clear from the documentation.

On Tue, Feb 17, 2009 at 9:13 PM, Esmail Bonakdarian <esmail...@gmail.com>wrote:

> (sorry if this is a duplicate-problems with posting at my end)
> ----
> Hello all,
>
> I need some help with a nice R-idiomatic and efficient solution to a
> small problem.
>
> Essentially, I am trying to eliminate randomly half of the entries in
> a vector that contains index values into some other vectors.
>
> More details:
>
> I am working with two strings/vectors of 0s and 1s. These will contain
> about 200 elements (always the same number for both)
>
> I want to:
>
> 1. determines the locations of where the two strings differ
>
>    --> easy using xor(s1, s2)
>
> 2. *randomly* selects *half* of those positions
>
>    --> not sure how to do this. I suppose the result would be
>        a list of index positions of size sum(xor(s1, s2))/2
>
> 3. exchange (flip) the bits in those random positions for both strings
>
>    --> I have something that seems to do that, but it doesn't look
>        slick and I wonder how efficient it is.
>
> Mostly I need help for #2, but will happily accept suggestions for #3,
> or for that matter anything that looks odd.
>
> Below my partial solution .. the HUX function is what I am trying
> to finish if someone can point me in the right direction.
>
> Thanks
> Esmail
> ------
>
> rm(list=ls())
>
> ####################################################
> # create a binary vector of size "len"
> #
> create_bin_Chromosome <- function(len)
> {
>   sample(0:1, len, replace=T)
> }
>
> ####################################################
> # HUX - half uniform crossover
> #
> # 1. determines the locations of where the two strings
> #    differ (easy xor)
> #
> # 2. randomly selects half of those positions
> #
> # 3. exchanges (flips) the bits in those positions for
> #    both
> #
> HUX <- function(b1, b2)
> {
>   # 1. find differing bits
>   r=xor(b1, b2)
>
>   # positions where bits differ
>   different = which(r==TRUE)
>
>   cat("\nhrp: ", different, "\n")
>   # 2. ??? how to do this best so that each time
>   #    a different half subset is selected? I.e.,
>   #    sum(r)/2 positions.
>
>   # 3. this flips *all* positions, should really only flip
>   #    half of them (randomly selected half)
>   new_b1 = b1
>   new_b2 = b2
>
>   for(i in different)  # should contain half the entries (randomly)
>       {
>         new_b1[i] = b2[i]
>         new_b2[i] = b1[i]
>       }
>
>   result <- matrix(c(new_b1, new_b2), 2, LEN, byrow=T)
>   result
> }
>
> LEN = 5
> b1=create_bin_Chromosome(LEN)
> b2=create_bin_Chromosome(LEN)
>
> cat(b1, "\n")
> cat(b2, "\n")
>
> idx=HUX(b1, b2)
> cat("\n\n")
> cat(idx[1,], "\n")
> cat(idx[2,], "\n")
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to