Rev 49914 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/Comparison.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2009 R Core Development Team% Distributed under GPL 2 or later\name{Comparison}\alias{<}\alias{<=}\alias{==}\alias{!=}\alias{>=}\alias{>}\alias{Comparison}\alias{collation}\title{Relational Operators}\description{Binary operators which allow the comparison of values in atomic vectors.}\usage{x < yx > yx <= yx >= yx == yx != y}\arguments{\item{x, y}{atomic vectors, symbols, calls, or other objects for whichmethods have been written.}}\details{The binary comparison operators are generic functions: methods can bewritten for them individually or via the\code{\link[=S3groupGeneric]{Ops}}) group generic function. (See\code{\link[=S3groupGeneric]{Ops}} for how dispatch is computed.)Comparison of strings in character vectors is lexicographic within thestrings using the collating sequence of the locale in use: see\code{\link{locales}}. The collating sequence of locales such as\samp{en_US} is normally different from \samp{C} (which should useASCII) and can be surprising. Beware of making \emph{any} assumptionsabout the collation order: e.g. in Estonian \code{Z} comes between\code{S} and \code{T}, and collation is not necessarilycharacter-by-character -- in Danish \code{aa} sorts as a singleletter, after \code{z}. In Welsh \code{ng} may or may not be a singlesorting unit: if it is it follows \code{g}. Some platforms maynot respect the locale and always sort in numerical order of the bytesin an 8-bit locale, or in Unicode point order for a UTF-8 locale (andmay not sort in the same order for the same language in differentcharacter sets). Collation of non-letters (spaces, punctuation signs,hyphens, fractions and so on) is even more problematic.At least one of \code{x} and \code{y} must be an atomic vector, but ifthe other is a list \R attempts to coerce it to the type of the atomicvector: this will succeed if the list is made up of elements of lengthone that can be coerced to the correct type.If the two arguments are atomic vectors of different types, one iscoerced to the type of the other, the (decreasing) order of precedencebeing character, complex, numeric, integer, logical and raw.Missing values (\code{\link{NA}}) and \code{\link{NaN}} values areregarded as non-comparable even to themselves, so comparisonsinvolving them will always result in \code{NA}. Missing values canalso result when character strings are compared and one is not validin the current collation locale.Language objects such as symbols and calls are deparsed tocharacter strings before comparison.}\value{A logical vector indicating the result of the element by elementcomparison. The elements of shorter vectors are recycled asnecessary.Objects such as arrays or time-series can be compared this wayprovided they are conformable.}\note{Do not use \code{==} and \code{!=} for tests, such as in \code{if}expressions, where you must get a single \code{TRUE} or\code{FALSE}. Unless you are absolutely sure that nothing unusualcan happen, you should use the \code{\link{identical}} functioninstead.For numerical and complex values, remember \code{==} and \code{!=} donot allow for the finite representation of fractions, nor for roundingerror. Using \code{\link{all.equal}} with \code{identical} is almostalways preferable. See the examples.}\section{S4 methods}{These operators are members of the S4 \code{\link{Compare}} group generic,and so methods can be written for them individually as well as for thegroup generic (or the \code{Ops} group generic), with arguments\code{c(e1, e2)}.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.Collation of character strings is a complex topic. For anintroduction see\url{http://en.wikipedia.org/wiki/Collating_sequence}. The\emph{Unicode Collation Algorithm}(\url{http://unicode.org/reports/tr10/}) is likely to be increasinglyinfluential. Where available \R makes use of ICU(\url{http://site.icu-project.org/} for collation.}\seealso{\code{\link{factor}} for the behaviour with factor arguments.\code{\link{Syntax}} for operator precedence.\code{\link{icuSetCollate}} to tune the string collation algorithmwhen ICU is in use.}\examples{x <- stats::rnorm(20)x < 1x[x > 0]x1 <- 0.5 - 0.3x2 <- 0.3 - 0.1x1 == x2 # FALSE on most machinesidentical(all.equal(x1, x2), TRUE) # TRUE everywhere\donttest{z <- c(32:126, 160:255) # range of most 8-bit charsets, Latin-1 in Unicodex <- if(l10n_info()$MBCS) {intToUtf8(z, multiple = TRUE)} else rawToChar(as.raw(z), multiple= TRUE)## by numberwriteLines(strwrap(paste(x, collapse=" "), width = 60))## by locale collationwriteLines(strwrap(paste(sort(x), collapse=" "), width = 60))}}\keyword{logic}