Fire
Fire

Reputation: 311

How to write an if statement that stops code when any variable in a vector is outside of the limits provided?

How do I write an if statement that stops code when any variable in the vector is outside of the intended sequence?

y <- c(1,2,3,4,11)

if(y > 10 | y < 0) 
    stop("Score has to be between 0 and 10")

Would it be done through loops? If so, which loop?

Upvotes: 2

Views: 100

Answers (1)

akrun
akrun

Reputation: 887223

We can use

library(dplyr)
if(!all(between(y, 0, 10)) )

Or with stopifnot

stopifnot(all(y %in% 1:9))

Upvotes: 1

Related Questions