Hello all, I have the following function call to create a matrix of POP_SIZE rows and fill it with bit strings of size LEN: pop=create_pop_2(POP_SIZE, LEN) I have 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 it be reflected in 'keep_pop' too? (I don't think so, but just wanted to check). I would like two independent copies. (2) If I wanted to change the order of *rows* in my matrix 'pop', is there an easy way to shuffle these? I don't want to change anything in the columns, just the complete rowsn (E.g., in Python I could just say something like suffle(pop) assuming pop is a list of list) - is there an equivalent for R? (3) I would like to compare the contents of 'keep_pop' with 'pop'. Though the order of rows may be different it should not matter as long as the same rows are present. Again, 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? Thanks, Esmail --------------- the code called above ------------- #################################################### # create a 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 }
David Winsemius
2009-Apr-26 05:11 UTC
[R] 3 questions regarding matrix copy/shuffle/compares
On Apr 26, 2009, at 12:28 AM, Esmail wrote:> Hello all, > > I have the following function call to create a matrix of POP_SIZE rows > and fill it with bit strings of size LEN: > > pop=create_pop_2(POP_SIZE, LEN)Are you construction a vector or a matrix? What are the dimensions of your matrix?> > > I have 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 it be reflected in 'keep_pop' > too? (I don't think so, but just wanted to check). I would like > two independent copies."==" 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".> > > (2) If I wanted to change the order of *rows* in my matrix 'pop', is > there > an easy way to shuffle these? I don't want to change anything in > the > columns, just the complete rowsn (E.g., in Python I could just say > something like suffle(pop) assuming pop is a list of list) - is > there > an equivalent for R?You can get a value from a matrix by using the indexing construction. But your terminology is confusing. Is pop a matrix or a list? ?"[" ?order ... and perhaps ?sample if you wanted a random permutation of the rows. 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' with 'pop'. > Though > the order of rows may be different it should not matter as long as > the same rows are present. Again, 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?Depends on what you want to do and what you are doing it on. You could look at: ?%in% ?merge> > > Thanks, > > Esmail > > --------------- the code called above -------------The code below creates a "bit vector" but then only makes exact multiples of it in the first row and zeros in the second row. Was that what was desired?> #################################################### > # create a 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 > } >David Winsemius, MD Heritage Laboratories West Hartford, CT
David Winsemius
2009-Apr-26 15:08 UTC
[R] 3 questions regarding matrix copy/shuffle/compares
On Apr 26, 2009, at 9:43 AM, Esmail wrote:> David Winsemius wrote: >> 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." > > > The code I have provided works, here is a run that may prove helpful: > > POP_SIZE = 6 > LEN = 8 > > pop=create_pop_2(POP_SIZE, LEN) > > print(pop) > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] > [1,] 0 1 0 1 1 0 0 1 > [2,] 0 0 0 0 0 0 0 0 > [3,] 1 1 0 0 1 0 0 0 > [4,] 0 0 0 0 0 0 0 1 > [5,] 0 0 1 1 0 0 1 0 > [6,] 1 0 0 0 0 0 1 0 > [7,] 0 0 0 0 0 0 0 0 > [8,] 0 0 0 0 0 0 0 0 > [9,] 0 0 0 0 0 0 0 0 > [10,] 0 0 0 0 0 0 0 0 > [11,] 0 0 0 0 0 0 0 0 > [12,] 0 0 0 0 0 0 0 0 > > I want to (1) create a deep copy of pop,I have already said *I* do not know how to create a "deep copy" in R.> (2) be able to shuffle the rows only, andI have suggested that shuffling by way of a random selection of an external index: > pop=create_pop_2(POP_SIZE, LEN) [1] 48 > pop [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1 1 0 0 1 0 1 1 [2,] 1 0 1 0 0 0 1 0 [3,] 1 1 0 1 0 1 0 0 [4,] 0 0 0 0 1 0 0 0 [5,] 1 0 0 1 1 1 1 1 [6,] 1 1 0 0 0 0 0 0 [7,] 0 0 0 0 0 0 0 0 [8,] 0 0 0 0 0 0 0 0 [9,] 0 0 0 0 0 0 0 0 [10,] 0 0 0 0 0 0 0 0 [11,] 0 0 0 0 0 0 0 0 [12,] 0 0 0 0 0 0 0 0 > dx <- sample(1:nrow(pop), nrow(pop) ) > dx [1] 12 10 8 9 3 1 6 11 5 7 4 2 > pop[dx,] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 0 0 0 0 0 0 0 0 [2,] 0 0 0 0 0 0 0 0 [3,] 0 0 0 0 0 0 0 0 [4,] 0 0 0 0 0 0 0 0 [5,] 1 1 0 1 0 1 0 0 [6,] 1 1 0 0 1 0 1 1 [7,] 1 1 0 0 0 0 0 0 [8,] 0 0 0 0 0 0 0 0 [9,] 1 0 0 1 1 1 1 1 [10,] 0 0 0 0 0 0 0 0 [11,] 0 0 0 0 1 0 0 0 [12,] 1 0 1 0 0 0 1 0> (3) be able to compare two copies of these objects > for equality and have it return True if only the rows have been > shuffled.I see two possible questions, the first easier (for me) than the second. Do you want to work on a copy with a known permutation of rows... or on a copy with an unknown ordering? In the first case I am unclear why you would not create an original and a copy, work on the copy, and compare with the original that is also sorted by the external index.> > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
hadley wickham
2009-Apr-26 15:28 UTC
[R] 3 questions regarding matrix copy/shuffle/compares
>> I want to (1) create a deep copy of pop, > > I have already said *I* do not know how to create a "deep copy" in R.Creating a deep copy is easy, because all copies are "deep" copies. You need to try very hard to create a reference in R. Hadley -- http://had.co.nz/