Babak Fi Foo
Babak Fi Foo

Reputation: 1048

How to validate if all elements in a vector equal to a scalar in R?

lets assume we have this vector:

v <- c(1,1,1,1,1)

Is there a function that validates if all elements in a vector are equal to a scallar?

In other words v == 1 returns TRUE ?

Upvotes: 1

Views: 217

Answers (2)

neilfws
neilfws

Reputation: 33772

I think you can use all ?

all(c(1, 1, 1, 1, 1) == 1)
[1] TRUE

all(c(1, 1, 1, 1, 2) == 1)
[1] FALSE

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520908

You could use min() and max():

v <- c(1,1,1,1,1)
max(v) == 1 && min(v) == 1

[1] TRUE

The logic here is that if the smallest and largest values in the incoming vector are both 1, then all values must 1.

Upvotes: 1

Related Questions