Vivek Ranjan
Vivek Ranjan

Reputation: 1492

Checking the value in a vector in R

Refer the below code. id <- 1:10. How the warning can be avoided?

allFiles <- list.files(directory)

fileRange <- c(1:length(allFiles))
if(!(as.numeric(id) %in% fileRange))
{
  print("Invalid file range")
  stop()
}
Warning: the condition has length > 1 and only the first element will be used

Upvotes: 2

Views: 85

Answers (2)

Ven Yao
Ven Yao

Reputation: 3710

Other roads:

if ( all( is.element(x, y) ) )

if ( setequal( x, intersect(x, y) ) )

Upvotes: 0

Frank
Frank

Reputation: 66819

To check whether one vector lies entirely inside another, use all:

if ( all(x %in% y) )

Upvotes: 6

Related Questions