首先是从别人那里偷来的例子:
> x <- c(0,1,1,0,1)
> y <- c(1,0,1,0,1)
> x %in% y # 这种算法是判断x的每个元素是否在y中出现。
[1] TRUE TRUE TRUE TRUE TRUE
> x == y # 判断每个相对应的元素是否相等。
[1] FALSE FALSE TRUE TRUE TRUE
>which(x==y)
[1] 2 3 4
这个方法很简单,但是有精度问题,比如/x *x 这个操作后,可能和原结果就不等了
于是有下面这个函数:
近似相等
all.equal(x,y):可以设置容忍的误差值,用于近似比较
x,y,可以是list,vector
返回mae
e.g. isTrue(all.equal(x,y))
########################
all.equal(target, current, ...)
## S3 method for class ’numeric’
all.equal(target, current,
tolerance = .Machine$double.eps ^ 0.5,
scale = NULL, check.attributes = TRUE, ...)
Arguments
target:
R object.
current:
other R object, to be compared with target.
... Further arguments for different methods, notably the following two, for numerical
comparison:
tolerance:
numeric >= 0. Differences smaller than tolerance are not considered.
scale:
numeric scalar > 0 (or NULL). See ‘Details’.
check.attributes:
logical indicating if the attributes(.) of target and current should be
compared as well.
check.names:
logical indicating if the names(.) of target and current should be compared
as well (and separately from the attributes).
######
精确相等:
identical(x,y):语法类似以上