Comparison of elements of a vector

Asked

Viewed 82 times

3

Staff need a function to check that all vector coordinates are equal.

a = rep(1, 5)

Let the previous output be TRUE.

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

Let the previous output be FALSE.

2 answers

1

I get it,

all(a[1] == a)

and

all(b[1] == b)

1

Complementing Wagner’s answer. It’s correct, but it doesn’t work if the vectors contain values NA.

> 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.