Q:

Applying '>' in if condition with a vector larger than length 1 in R

I have a vector of size 5 e.g.:
 a<-c(1,4,6,3,2)

I also have another vector of size 1: 
 b<-9

I would like to write the following if condition:
if (a>b) { 1
}
else 0
}

I get the following warning: 
 Warning message:
In if (fitness_neighbours > user_fitness) { :
  the condition has length > 1 and only the first element will be used

What I would actually like it to do is to check if any of the elements in 'a' satisfy the condition.

A:

Use any() and a comparison:
if(any(a > b)) {
  # Executes if any value in a > b.
} else {
  # No a is greater than b.
} 

Another way of doing it using pmax():
if (any(pmax(a, b) == a)) {

} else {

} 

That's saying that if any of the max of (a, b) is equal to the value in a, then a must be greater.

A:

Just use simple comparison:
a <- c(1,4,6,3,20)
b <- 9
a > b

[1] FALSE FALSE FALSE FALSE  TRUE

This works because R is fundamentally a vector-based language.
You can easily convert the logical result to numeric:
as.numeric(a > b)
[1] 0 0 0 0 1

