[R] Equality of Vectors

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Fri Aug 13 20:55:39 CEST 2010


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



More information about the R-help mailing list