Hello, Is there a way to get a single TRUE or FALSE statement from comparing two vectors? For example, c(1,2,3) == c(1,2,3) produces TRUE TRUE TRUE where I would like it to produce only TRUE for use in an if statement. Likewise, when two vectors are not exactly identical (in all elements) I would like a single FALSE result, as opposed to c(1,2,3) == c(1,2,5) TRUE TRUE FALSE Any ideas? Thanks, Mitch
?all - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Fri, 13 Aug 2010, Downey, Patrick wrote:> Hello, > > Is there a way to get a single TRUE or FALSE statement from comparing two > vectors? For example, > c(1,2,3) == c(1,2,3) > produces > TRUE TRUE TRUE > > where I would like it to produce only > TRUE > for use in an if statement. > > Likewise, when two vectors are not exactly identical (in all elements) I > would like a single FALSE result, as opposed to > c(1,2,3) == c(1,2,5) > TRUE TRUE FALSE > > Any ideas? > > Thanks, > Mitch > > ______________________________________________ > 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. >
Hi Mitch, How about identical()?> identical(c(1,2,3), c(1,2,5))[1] FALSE> identical(c(1,2,3), c(1,2,3))[1] TRUE See ?identical and ?all.equal for more information. HTH, Jorge On Fri, Aug 13, 2010 at 2:49 PM, Downey, Patrick <> wrote:> Hello, > > Is there a way to get a single TRUE or FALSE statement from comparing two > vectors? For example, > c(1,2,3) == c(1,2,3) > produces > TRUE TRUE TRUE > > where I would like it to produce only > TRUE > for use in an if statement. > > Likewise, when two vectors are not exactly identical (in all elements) I > would like a single FALSE result, as opposed to > c(1,2,3) == c(1,2,5) > TRUE TRUE FALSE > > Any ideas? > > Thanks, > Mitch > > ______________________________________________ > 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]]
?all nikhil.list at gmail.com On Aug 13, 2010, at 2:49 PM, Downey, Patrick wrote:> c(1,2,3) == c(1,2,3)
Patrick, See all(). For example,> all(c(1,2,3)==c(1,2,3))[1] TRUE> all(c(1,2,3)==c(2,1,3))[1] FALSE> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Downey, Patrick > Sent: Friday, August 13, 2010 1:49 PM > To: r-help at r-project.org > Subject: [R] Equality of Vectors > > Hello, > > Is there a way to get a single TRUE or FALSE statement from comparing > two > vectors? For example, > c(1,2,3) == c(1,2,3) > produces > TRUE TRUE TRUE > > where I would like it to produce only > TRUE > for use in an if statement. > > Likewise, when two vectors are not exactly identical (in all elements) > I > would like a single FALSE result, as opposed to > c(1,2,3) == c(1,2,5) > TRUE TRUE FALSE > > Any ideas? > > Thanks, > Mitch > > ______________________________________________ > 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.
all(c(1, 2, 3) %in% c(1,2,3)) On Fri, Aug 13, 2010 at 3:49 PM, Downey, Patrick <PDowney@urban.org> wrote:> Hello, > > Is there a way to get a single TRUE or FALSE statement from comparing two > vectors? For example, > c(1,2,3) == c(1,2,3) > produces > TRUE TRUE TRUE > > where I would like it to produce only > TRUE > for use in an if statement. > > Likewise, when two vectors are not exactly identical (in all elements) I > would like a single FALSE result, as opposed to > c(1,2,3) == c(1,2,5) > TRUE TRUE FALSE > > Any ideas? > > Thanks, > Mitch > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
have a look at function all() but also at function all.equal() -- for your example, x1 <- c(1,2,3) x2 <- c(1,2,3) x3 <- c(1,2,5) all(x1 == x2) all(x1 == x3) # this safer isTRUE(all.equal(x1, x2)) isTRUE(all.equal(x1, x3)) I hope it helps. Best, Dimitris On 8/13/2010 8:49 PM, Downey, Patrick wrote:> Hello, > > Is there a way to get a single TRUE or FALSE statement from comparing two > vectors? For example, > c(1,2,3) == c(1,2,3) > produces > TRUE TRUE TRUE > > where I would like it to produce only > TRUE > for use in an if statement. > > Likewise, when two vectors are not exactly identical (in all elements) I > would like a single FALSE result, as opposed to > c(1,2,3) == c(1,2,5) > TRUE TRUE FALSE > > Any ideas? > > Thanks, > Mitch > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014