[R] Equality of multiple vectors

Petr Savicky savicky at cs.cas.cz
Fri May 4 11:36:07 CEST 2012


On Fri, May 04, 2012 at 12:53:12AM -0700, aaurouss wrote:
> Hello,
> 
> I'm writing a piece of code where I need to compare multiple same length
> vectors. 
> 
> I've gone through the basic functions like identical() or all(), but they
> only work for comparing 2 vectors. From 3 vectors on, it doesn't work .
> 
> Example: Assuming  
> vec1 <- c (1,2,3,4,5)
> vec2 <- c(1,2,3,4,5)
> vec3 <- c(1,2,3,4,4)
> 
> identical (vec1,vec2,vec3) returns TRUE, since the 2 first vectors are
> equal. I need a function that returns FALSE if one of the vectors is
> different.

Hi.

Try the following.

  length(unique(list(vec1, vec2, vec3))) == 1

  [1] FALSE

  length(unique(list(vec1, vec2, vec1))) == 1

  [1] TRUE

Hope this helps.

Petr Savicky.



More information about the R-help mailing list