From: Jim Lemon <jim_at_bitwrit.com.au>

Date: Fri, 06 Jun 2008 19:54:13 +1000

*>
*

> [1] "yes"

*>
*

*>
*

*> [1] "blah"
*

*>
*

*>
*

> Only replace the numbers with for instance the vectors

*>
*

*>
*

*>
*

*>
*

*>
*

> Now, I realize I only get a warning when comparing things, but this to

*> me means that I am not doing it correctly:
*

*>
*

*>
*

*>
*

> [1] "yes"

*>
*

*>
*

*> [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
*

*>
*

*>
*

*> [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:

*>
*

*>
*

*>
*

*> [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

*>
*

*>
*

*> [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?

*>
*

Hi Karin,

My interpretation of your question is that you want to test whether two vectors contain the same elements, whether or not the order of those elements is the same. I'll first assume that the vectors must only have elements from the same _set_ and it doesn't matter if they have different lengths.

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 Fri 06 Jun 2008 - 13:25:24 GMT

Date: Fri, 06 Jun 2008 19:54:13 +1000

Karin Lagesen wrote:

> 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")

> 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

>>if (a==a) show("yes") else show("blah")

> [1] "yes"

>>if (a==b) show("yes") else show("blah")

> In if (a == b) show("yes") else show("blah") :

>>if (b == c) show("yes") else show("blah")

> I have also tried the %in% comparator, but that one throws warnings too:

>>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")

> So, how is this really supposed to be done?

Hi Karin,

My interpretation of your question is that you want to test whether two vectors contain the same elements, whether or not the order of those elements is the same. I'll first assume that the vectors must only have elements from the same _set_ and it doesn't matter if they have different lengths.

if(length(unique(a))==length(unique(b))) {
if(all(unique(a)==unique(b))) cat("Yes\n")
else cat("No\n")

}

else cat("No\n")

However, if the lengths must be the same, but the order may be different:

if(length(a)==length(b)) {

if(all(sort(a)==sort(b))) cat("Yes\n")
else cat("No\n")

}

else cat("No\n")

The latter test ensures that if there are repeated elements, the number of repeats of each element is the same in each vector.

Jim

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 Fri 06 Jun 2008 - 13:25:24 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 Fri 06 Jun 2008 - 13:30:37 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.
*