Comparison
Comparison Operators

Description

Returns a vector of logical values, which is the result of the comparison applied to each element of the input vectors.

Usage

e1 op e2

Arguments

op one of >, <, >=, <=, == or !=.

The comparison operators are:

OPERATOR MEANING
> greater than.
< less than.
>= greater than or equal to.
<= less than or equal to.
== equal to. Can accept complex vectors.
!= not equal to. Can accept complex vectors.
e1, e2 numeric or character vectors. Can be complex vectors if you are using with == or !=. Missing values (NAs) are allowed.

Details

An NA element of e1 or e2 results in an NA element for the answer.
These comparison operator functions are members of the Ops group of generic functions.
For character data, ordering is by the ASCII character set.
See section 5.6 of Becker, Chambers and Wilks (1988) for the rules for dealing with operands with attributes.
Value
logical vector with FALSE or TRUE in each element, according to the truth of the element-wise comparison of the operands.
Warning
You must leave a space between a "<" operator and a negative number because "<-" is always interpreted as the assignment operator. This is a very unusual case; spaces usually make no difference.
Classes
This function is used as the default method for classes that do not inherit a specific method for the function or for the Ops group of functions. The result retains the class and the attributes. If this behavior is not appropriate, the designer of the class should provide a method for the function or for the Ops group.
References
Becker, R.A., Chambers, J.M., and Wilks, A.R. (1988). The New S Language Wadsworth and Brooks/Cole, Pacific Grove, CA.
Note
To test for missing values, use is.na.
To test for a NULL value, use is.null.
When comparing floating point numbers, you might want to allow for small differences. For example, use abs(x-y) < 1E-6, rather than testing for exact equality using ==.
identical(x,y) is useful in many cases in place of x==y. For example, use for non-numerical objects, or to compare objects of different lengths.
See Also
Compare, compare, Logic, match, identical.na, is.na, is.null, sign.
Examples
a <- c(1,2,3,4)
b <- c(4,3,2,1)
a > b      # true when 'a' greater than 'b' 
# [1] FALSE FALSE  TRUE  TRUE

x <- c(2,4,6,8,NA) x == NA # the wrong way to find missing elements # [1] NA NA NA NA NA

is.na(x) # the correct way to find missing elements # [1] FALSE FALSE FALSE FALSE TRUE

x[x > 5] # all 'x' values that are NA or larger than 5 # [1] 6 8 NA x[x > 5 & !is.na(x)] # all non-NA 'x' values larger than 5 # [1] 6 8

x == NULL # the wrong way to check if an object is NULL # logical(0)

is.null(x) # the correct way to check whether an object is NULL # [1] FALSE identical(x, NULL) # another way to check for NULL # [1] FALSE

# one way to check if x is a single TRUE value length(x)==1 && class(x) == "logical" && x # [1] FALSE identical(x, TRUE) # an easier way # [1] FALSE

Package base version 6.1.4-13
Package Index