[R] element-by-element comparison

Tsjerk Wassenaar tsjerkw at gmail.com
Sun Oct 30 09:51:59 CET 2011


Hi,

To compare row wise is merely to compare column wise using the transpose matrix:

t(B) < A

or

t(t(B)<A)

if the result needs to be a matrix with dimensions equal to B.

Cheers,

Tsjerk

On Sun, Oct 30, 2011 at 9:44 AM, Patrick Burns <pburns at pburns.seanet.com> wrote:
> Given that you want to compare
> columns, you can just do:
>
> A > B
>
> If you wanted to compare rows, then
> it is more troublesome.  One approach
> would be:
>
> rep(A, each=nrow(B)) > B
>
>
> On 30/10/2011 03:51, Wendy wrote:
>>
>> Hi,
>>
>> I have a vector and a matrix. For example,
>>
>> A = [
>> 12
>> 3
>> 4];
>>
>> B = [
>> 4       13
>> 10      2
>> 4       8];
>>
>> I am comparing A to each column of B using A>B[,ii], so the expected
>> result
>> is
>>
>> C = [
>> 1    0
>> 0    1
>> 0    0];
>>
>> I am looking for a way to do this quickly instead of going through the for
>> loop, but haven't had any luck yet? Any advice is appreciated.
>>
>> Thank you very much.
>>
>> Wendy
>>
>>
>>
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/element-by-element-comparison-tp3952301p3952301.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>>
>
> --
> Patrick Burns
> pburns at pburns.seanet.com
> twitter: @portfolioprobe
> http://www.portfolioprobe.com/blog
> http://www.burns-stat.com
> (home of 'Some hints for the R beginner'
> and 'The R Inferno')
>
> ______________________________________________
> 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.
>



-- 
Tsjerk A. Wassenaar, Ph.D.

post-doctoral researcher
Molecular Dynamics Group
* Groningen Institute for Biomolecular Research and Biotechnology
* Zernike Institute for Advanced Materials
University of Groningen
The Netherlands



More information about the R-help mailing list