Comparison {base} | R Documentation

## 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, or other objects for which methods have been written.

### Details

The binary comparison operators are generic functions: methods can be written for them individually or via the `Ops`) group generic function.

Comparison of strings in character vectors is lexicographic within the strings using the collating sequence of the locale in use: see `locales`. The collating sequence of locales such as `en_US` is normally different from `C` (which should use ASCII) and can be surprising.

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 canbe coerced to the correct type.

If the two arguments are atomic vectors of different types, they are both coerced to the first of character, complex, numeric, integer and logical.

### Value

A vector of logicals 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.

### Note

Don't use `==` and `!=` for tests, such as in `if` expressions, where you must get a single `TRUE` or `FALSE`. Unless you are absolutely sure that nothing unusual can happen, you should use the `identical` function instead.

For numerical values, remember `==` and `!=` do not allow for the finite representation of fractions, nor for rounding error. Using `all.equal` with `identical` is almost always preferable. See the examples.

### References

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

### See Also

`Syntax` for operator precedence.

### Examples

    x <- rnorm(20)
    x < 1
    x[x > 0]

    x1 <- 0.5 - 0.3
    x2 <- 0.3 - 0.1
    x1 == x2                           # FALSE on most machines
    identical(all.equal(x1, x2), TRUE) # TRUE everywhere

[Package base version 2.2.1 Index]