[R] vector comparison

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Thu Jun 5 14:03:28 CEST 2008


probably you want to look at:

?any
?all


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Karin Lagesen" <karinlag at studmed.uio.no>
To: <r-help at r-project.org>
Sent: Thursday, June 05, 2008 12:38 PM
Subject: [R] vector comparison


>
> I know this is fairly basic, but I must have somehow missed it in 
> the
> manuals.
>
> I have two vectors, often of unequal length. I would like to compare
> them for identity. Order of elements do not matter, but they should
> contain the same.
>
> I.e: I want this kind of comparison:
>
>> if (1==1) show("yes") else show("blah")
> [1] "yes"
>> if (1==2) show("yes") else show("blah")
> [1] "blah"
>>
>
> Only replace the numbers with for instance the vectors
>
>> a = c("a")
>> b = c("b","c")
>> c = c("c","b")
>
>
> Now, I realize I only get a warning when comparing things, but this 
> to
> me means that I am not doing it correctly:
>
>> if (a==a) show("yes") else show("blah")
> [1] "yes"
>> if (a==b) show("yes") else show("blah")
> [1] "blah"
> Warning message:
> In if (a == b) show("yes") else show("blah") :
>  the condition has length > 1 and only the first element will be 
> used
>>
>> if (b == c) show("yes") else show("blah")
> [1] "blah"
> Warning message:
> In if (b == c) show("yes") else show("blah") :
>  the condition has length > 1 and only the first element will be 
> used
>>
>
> I have also tried the %in% comparator, but that one throws warnings 
> too:
>
>> if (b %in% c) show("yes") else show("blah")
> [1] "yes"
> Warning message:
> In if (b %in% c) show("yes") else show("blah") :
>  the condition has length > 1 and only the first element will be 
> used
>>
>> if (c %in% c) show("yes") else show("blah")
> [1] "yes"
> Warning message:
> In if (c %in% c) show("yes") else show("blah") :
>  the condition has length > 1 and only the first element will be 
> used
>>
>
> So, how is this really supposed to be done?
>
> Thanks!
>
> Karin
>
> ______________________________________________
> 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list