The code that will deliver the expected answer to the implicit question in your first sentence is:
1 %in% 2:4
[1] FALSE
h=1
h %in% 1:2
[1] TRUE
The "==" operator does not implicitly generate a range (or a vector of alternatives) from a logical conjunction. It is considered a "Comparison"-operator and is "generic" which will mean that methods can be different for different data-types. They are also vectorized (with the implicit recycling rules in force) so may return many values:
(1:2) == (1:4)
[1] TRUE TRUE FALSE FALSE
And the as.numeric()
-function applied to the value 1
will return exactly the input.
Further comment: Since your attempted use of "==" was really as an implicit set operation, you might want to review the help page for ?intersect
. There're also packages that handle more sophisticated set functions.
R's coercion can go back and forth between logical and numeric depending on how functions are defined:
sum( letters[1:10] %in% letters)
[1] 10
sum( letters[1:10] == letters)
[1] 10
Warning message:
In letters[1:10] == letters :
longer object length is not a multiple of shorter object length