Boolean indexing, similar to answer from @Konrad.Rudolph, using tolerance based on default for ?all.equal
:
tol <- 0.5 * .Machine$double.eps # tolerance close to machine precision
float.equal <- function(a, b, p.tol = tol) { abs(a - b) < p.tol }
x[float.equal(x, -273.17)] <- NA
Consider the following if your requirement can be reduced to an easier-to-code predicate, e.g.:
x[x<0] <- NA
all.equal
works on vectors of equal length, and reports on the equality of full objects being compared, not element-wise equality.
For R version 3.0.1 (2013-05-16), isTRUE(all.equal())
does not produce the desired result:
> all.equal(x, -273.17)
[1] "Numeric: lengths (5, 1) differ"
> isTRUE(all.equal(x, -273.17))
[1] FALSE