[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  Comparison: Relational Operators

##  Description

Binary operators which allow the comparison of values in atomic vectors.

##  Usage

    
    
    x < y
    x > y
    x <= y="" x="">= y
    x == y
    x != y

##  Arguments

x, y

atomic vectors, symbols, calls, or other objects for which methods have been
written.

##  Value

A logical vector indicating the result of the element by element comparison.
The elements of shorter vectors are recycled as necessary.

Objects such as arrays or time-series can be compared this way provided they
are conformable.

##  S4 methods

These operators are members of the S4 ` [ Compare
](/link/Compare?package=base&version=3.6.2) ` group generic, and so methods
can be written for them individually as well as for the group generic (or the
` Ops ` group generic), with arguments ` c(e1, e2) ` .

##  Details

The binary comparison operators are generic functions: methods can be written
for them individually or via the ` [ Ops
](/link/Ops?package=base&version=3.6.2&to=%3DS3groupGeneric) ` group generic
function. (See ` [ Ops
](/link/Ops?package=base&version=3.6.2&to=%3DS3groupGeneric) ` for how
dispatch is computed.)

Comparison of strings in character vectors is lexicographic within the strings
using the collating sequence of the locale in use: see ` [ locales
](/link/locales?package=base&version=3.6.2) ` . The collating sequence of
locales such as  en_US  is normally different from  C  (which should use
ASCII) and can be surprising. Beware of making _any_ assumptions about the
collation order: e.g.in Estonian ` Z ` comes between ` S ` and ` T ` , and
collation is not necessarily character-by-character -- in Danish ` aa ` sorts
as a single letter, after ` z ` . In Welsh ` ng ` may or may not be a single
sorting unit: if it is it follows ` g ` . Some platforms may not respect the
locale and always sort in numerical order of the bytes in an 8-bit locale, or
in Unicode code-point order for a UTF-8 locale (and may not sort in the same
order for the same language in different character sets). Collation of non-
letters (spaces, punctuation signs, hyphens, fractions and so on) is even more
problematic.

Character strings can be compared with different marked encodings (see ` [
Encoding ](/link/Encoding?package=base&version=3.6.2) ` ): they are translated
to UTF-8 before comparison.

Raw vectors should not really be considered to have an order, but the numeric
order of the byte representation is used.

At least one of ` x ` and ` y ` must be an atomic vector, but if the other is
a list  R  attempts to coerce it to the type of the atomic vector: this will
succeed if the list is made up of elements of length one that can be coerced
to the correct type.

If the two arguments are atomic vectors of different types, one is coerced to
the type of the other, the (decreasing) order of precedence being character,
complex, numeric, integer, logical and raw.

Missing values ( ` [ NA ](/link/NA?package=base&version=3.6.2) ` ) and ` [ NaN
](/link/NaN?package=base&version=3.6.2) ` values are regarded as non-
comparable even to themselves, so comparisons involving them will always
result in ` NA ` . Missing values can also result when character strings are
compared and one is not valid in the current collation locale.

Language objects such as symbols and calls are deparsed to character strings
before comparison.

##  References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole.

Collation of character strings is a complex topic. For an introduction see [
https://en.wikipedia.org/wiki/Collating_sequence
](https://en.wikipedia.org/wiki/Collating_sequence) . The _Unicode Collation
Algorithm_ ( [ http://unicode.org/reports/tr10/
](http://unicode.or