Comparison of elements of a vector

3

I need a function that checks if all the coordinates of the vector are equal.

a = rep(1, 5)

Let the previous output be TRUE.

b = c(rep(1, 4),2)

Let the previous output be FALSE.

    
asked by anonymous 26.09.2015 / 02:54

2 answers

1

I understood,

all(a[1] == a)

and

all(b[1] == b)
    
26.09.2015 / 03:23
1

Complementing Wagner's response. It is correct, but it does not work if the vectors contain NA values.

> a = rep(NA, 5)
> all(a[1] == a)
[1] NA
> b = c(rep(1, 4),NA)
> all(b[1] == b)
[1] NA

In this case you could use the following comparisons:

> isTRUE(all.equal(rep(a[1], length(a)), a))
[1] TRUE
> isTRUE(all.equal(rep(b[1], length(b)), b))
[1] FALSE
    
26.09.2015 / 21:44