R语言中,判断相等

首先是从别人那里偷来的例子:

> 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

这里,对于logic vector,可以用which函数选出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):语法类似以上

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值