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
This may help:> a <- c('a') > b <- c('a','b','c') > c <- c('a','b','d') > all(a %in% b)[1] TRUE> all(b %in% a)[1] FALSE> all(b %in% c)[1] FALSE> d <- c('b', 'c') > all(d %in% b)[1] TRUE>What you probably want to insure that the vectors contain the same elements is: if (all(v1 %in$ v2) && all(v2 %in% v1)) On Thu, Jun 5, 2008 at 6:38 AM, Karin Lagesen <karinlag@studmed.uio.no> 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") > [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@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
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
On 6/5/2008 6:38 AM, 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") > [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?a <- c("a") b <- c("b","c") c <- c("c","b") if (setequal(a, b)) show("yes") else show("blah") [1] "blah" if (setequal(a, c)) show("yes") else show("blah") [1] "blah" if (setequal(b, c)) show("yes") else show("blah") [1] "yes" ?setequal> 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Hello Karin This is your code : a = c("a") b = c("b","c") c = c("c","b") if (a==a) show("yes") else show("blah") if (a==b) show("yes") else show("blah") if (b==c) show("yes") else show("blah") Have a look at the conditions (a==b) and (b==c)> a==b[1] FALSE FALSE> b==c[1] FALSE FALSE They are size 2. I think R takes the first logical value. That's why you get the warning. I hope this helps. 2008/6/5 Karin Lagesen <karinlag@studmed.uio.no>:> > 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@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. >[[alternative HTML version deleted]]
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") > > [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? >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