Look at: identical(pop, pop2) # if you want to do "exact" comparison
Or all.equal(pop, pop2, tolerance=1.e-10) # if you want to allow for some small noise Ravi. -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Esmail Sent: Thursday, March 11, 2010 7:38 AM To: r-help@r-project.org Subject: [R] Comparing matrices Hello all, I have two matrices, pop and pop2, each the same number of rows and columns that I want to compare for equality. I am concerned about efficiency in this operation. I've tried a few things without success so far. Doing something simple like: if (pop==pop2) { cat('equal') } else { cat('NOT equal') } results in the warning: 1: In if (pop == pop2) { : the condition has length > 1 and only the first element will be used so it seems to look only at the first element. print(pop==pop2) gives me a listing of TRUE/FALSE values for each element. I am really only looking for a single TRUE or FALSE value to tell me if the two populations (ie pop and pop2) are the same or different. In my application there will be about 200 columns and 50 rows and this comparison will happen very frequently (possibly a few thousand times), so I am concerned about efficiency. I am appending the code I used to generate my matrices and some things I tried (mentioned above) and also the output get so far. FWIW, Linux environment, R 2.10.1. Thanks, Esmail ps: Am I correct that if I do the assignment pop2 = pop I create a totally separate instance/(deep)copy of the data? I tried a few tests that seem to confirm this, but I'd rather be sure. ------- code ------------ # create a binary vector of size "len" create_bin_Chromosome <- function(len) { sample(0:1, len, replace=T) } # create popsize members, each of length len create_pop_2 <- function(popsize, len) { datasize=len*popsize npop <- matrix(0, popsize, len, byrow=T) for(i in 1:popsize) npop[i,] = create_bin_Chromosome(len) npop } POP_SIZE = 3 LEN = 8 pop = create_pop_2(POP_SIZE, LEN) pop2 = pop print(pop==pop2) if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') } print(pop) print(pop2) pop[2,5] = 99 pop2[3,3] = 77 print(pop==pop2) if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') } print(pop) print(pop2) ------ output ----------- > source('popc.R') [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [2,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [3,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE equal [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 0 1 0 1 0 1 1 0 [2,] 0 1 1 0 0 0 0 0 [3,] 0 1 0 0 1 0 1 1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 0 1 0 1 0 1 1 0 [2,] 0 1 1 0 0 0 0 0 [3,] 0 1 0 0 1 0 1 1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [2,] TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE [3,] TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE equal [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 0 1 0 1 0 1 1 0 [2,] 0 1 1 0 99 0 0 0 [3,] 0 1 0 0 1 0 1 1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 0 1 0 1 0 1 1 0 [2,] 0 1 1 0 0 0 0 0 [3,] 0 1 77 0 1 0 1 1 Warning messages: 1: In if (pop == pop2) { : the condition has length > 1 and only the first element will be used 2: In if (pop == pop2) { : the condition has length > 1 and only the first element will be used > ______________________________________________ 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. ______________________________________________ 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.