[R] 3 questions regarding matrix copy/shuffle/compares

David Winsemius dwinsemius at comcast.net
Sun Apr 26 15:20:21 CEST 2009


On Apr 26, 2009, at 7:48 AM, Esmail wrote:

> Hello David,
>
> Let me try again, I don't think this was the best post ever I've  
> made :-)
> Hopefully this is clearer, or otherwise I may break this up into
> three separate simple queries as this may be too long.
>
>
> > "==" is not an assignment operator in R, so the answer is that it
> > would do neither.  "<-" and "=" can do assignment. In neither case
> > would it be a "deep copy".
>
> It was late when I posted the code, I made a mistake with regard to
> the assignment operator and used the boolean compare instead -- thanks
> for catching that.
>
> It should have been:
>
>    keep_pop[1:POP_SIZE] = pop[1:POP_SIZE]
>
>
> -------- Here's an edited and clearer version I hope:
>
>
> The basic idea is that I am trying to keep track of a number of  
> bitrings.
>
> Therefore I am creating a matrix (named 'pop') whose rows are made up
> of bit vectors (ie my bitstrings).  I only initialize half of the rows
> with my bitstrings of random 1s and 0s, the rest of the rows are set
> to all zeros).
>
> So I use following function call to create a matrix and fill it with
> bit strings:
>
>   pop=create_pop_2(POP_SIZE, LEN)
>
> where
>
>   POP_SIZE refers to the number of rows
>   LEN to the columns (length of my bitstrings)
>
>
>
> This is the code I call:
>
> ####################################################
> # create a random binary vector of size "len"
> #
> create_bin_Chromosome <- function(len)
> {
>  sample(0:1, len, replace=T)
> }
>
>
>
> ############## create_population ###################
> # create population of chromosomes of length len
> # the matrix contains twice as much space as popsize
> #
> create_pop_2 <- function(popsize, len)
> {
>  datasize=len*popsize
>  print(datasize)
>  npop <- matrix(0, popsize*2, len, byrow=T)
>
>  for(i in 1:popsize)
>    npop[i,] = create_bin_Chromosome(len)
>
>  npop
> }
>
>
> My 3 questions:
>
> (1) If I did
>
>    keep_pop[1:POP_SIZE] = pop[1:POP_SIZE]
>
>    to keep a copy of the original data structure before manipulating
>    'pop' potentially, would this make a deep copy or just shallow? Ie
>    if I change something in pop would keep_pop change too? I would
>    like two independent copies so that 'keep_pop' stays intact while
>    'pop' may change.
>
>    > "<-" and "=" can do assignment. In neither case would it be a
>    > "deep copy".
>
>    Is there a deepcopy operator, or would I have to have two nested
>    loops and iterate through them? Or is there a nice R-idiomatic way
>    to do this?

Not that I know of, although my knowledge of R depth is not  
encyclopedic. You might get the desired sort of effect by creating a  
copy  inside a function, working on it inside the function in the  
manner desired, and then comparing the output to the original. There  
might be other strategies to get certain effects by creating specific  
environments.

> (2) If I wanted to change the order of rows in my matrix 'pop', is
>    there an easy way to shuffle these?  I.e., I don't want to change
>    any of the bitstrings vectors/rows, just the order of the rows in  
> the
>    matrix 'pop'. (E.g., in Python I could just say something like
>    suffle(pop)) - is there an equivalent for R?
>
>    So if pop [ [0, 0, 0]
>                [1, 1, 1]
>                [1, 1, 0] ]
>
>    after the shuffle it may look like
>
>              [ [1, 1, 0]    (originally at index 2)
> 	        [1, 1, 1]    (originally at index 1)
>                [0, 0, 0] ]  (originally at index 0)
>
>    the rows themselves remained intact, just their order changes.
>    This is a tiny example, in my case I may have 100 rows (POPS_SIZE)
>    and rows of LEN 200.

Yes. As I said before "I am going to refrain from posting speculation  
until you provide valid R code
that will create an object that can be the subject of operations."
>
> (3) I would like to compare the contents of 'keep_pop' (a copy of the
>    original 'pop') with the current 'pop'. Though the order of rows
>    may be different between the two, it should not matter as long as
>    the same rows are present.  So for the example given above, the
>    comparison should return True.
>
>    For instance, in Python this would be simply
>
>    if sorted(keep_pop) == sorted(pop):
>       print 'they are equal'
>    else
>       print 'they are not equal'
>
>    Is there an equivalent R code segment?

If you created a random index vector that was used to sort the rows  
for display or computational purposes only, you could maintain the  
original ordering so that row wise comparisons could be done.
>
> I hope this post is clearer than my original one. Thank you David for
> pointing out some of the shortcomings of my earlier post.
>
> Thanks,
>
> Esmail

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list