Re: [R] Make one vector from matrix comparison

From: Stavros Macrakis <macrakis_at_alum.mit.edu>
Date: Wed, 12 Nov 2008 09:27:55 -0500

You can just call `c` on your result to flatten the matrix into a vector. You could also eliminate the for-loops by using the `apply` function:

pairwise_setequal <-

   function(a,b) c(apply(a, 1, function(r){ apply(b, 1, setequal, r ) } ))

But are you sure that is what you want to do? In the case of boolean vectors, setequal simply tests whether the two vectors include the same elements, i.e. both are empty, both contain only F, both contain only T, or both contain both F's and T's. If you want to compare the vectors pointwise, use `identical` instead of `setequal`.

             -s

> On Mon, Nov 10, 2008 at 10:11 AM, Chris82 <rubenbauar_at_gmx.de> wrote:

>> I compare each row of a matrix with each row of another matrix.
>>
>> testmat1 <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), nrow=4)
>> testmat2 <- matrix(c(1,2,3,5,5,6,7,8,9,10,11,12,13,14,15,16), nrow=4)
>>
>> Both matrix differs in the last row.
>>
>> Now I create a loop:
>>
>> for (i in (1:4)){
>> for (j in (1:4)){
>> b <- (c(setequal(testmat1[j,],testmat2[i,])))
>> print(b)
>> }
>> }
>>
>> R outputs me the following:
>>
>> [1] TRUE
>> [1] FALSE ....

>> but I need one vector like this:
>>
>> [1] TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
>> FALSE FALSE FALSE FALSE


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. Received on Wed 12 Nov 2008 - 14:29:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 12 Nov 2008 - 14:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive