ID EN
Vector & List

Comparison

R Base 3.6.2

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

Syntax

R
x < y
x > y
x <= y="" x="">= y
x == y
x != y</=>

Arguments

Parameter Description
x, y atomic vectors, symbols, calls, or other objects for which methods have been written.

Return 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.

Details

The binary comparison operators are generic functions: methods can be written for them individually or via the Ops group generic function. (See Ops 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. 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 numerica

Examples

Example
R
# NOT RUN {
x <- stats::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

# }
# NOT RUN {
# range of most 8-bit charsets, as well as of Latin-1 in Unicode
z <- c(32:126, 160:255)
x <- if(l10n_info()$MBCS) {
    intToUtf8(z, multiple = TRUE)
} else rawToChar(as.raw(z), multiple = TRUE)
## by number
writeLines(strwrap(paste(x, collapse=" "), width = 60))
## by locale collation
writeLines(strwrap(paste(sort(x), collapse=" "), width = 60))
# }

See Also

factor for the behaviour with factor arguments. Syntax for operator precedence. capabilities for whether ICU is available and icuSetCollate to tune the string collation algorithm when it is.