[英]Compare two vector in R
I have two vectors: a = c(1,2,3)
, b = c(1,2,3)
我有两个向量:
a = c(1,2,3)
, b = c(1,2,3)
I want to test whether a
is exactly the same as b
. 我想测试
a
是否与b
完全相同。 I know the result can be given by sum(a == b) == length(a)
, but is there any elegant way? 我知道结果可以用
sum(a == b) == length(a)
,但有没有优雅的方法?
We can use identical
我们可以使用
identical
identical(a,b)
#[1] TRUE
Or if we there are some difference in attributes which we need to avoid in the comparison, use all.equal
或者,如果我们在比较中需要避免的属性存在一些差异,请使用
all.equal
all.equal(a,b, check.attributes=FALSE)
#[1] TRUE
Or using similar approach in the OP's post, we can make it compact with all
或在OP的帖子使用类似的方法,我们可以把它结构紧凑,
all
all(a==b)
#[1] TRUE
The number of characters in the above approach is less... 上述方法中的字符数较少......
nchar("identical(a,b)")
#[1] 14
nchar("all(a==b)")
#[1] 9
In addition to the answer above; 除了上面的答案; you could also consider the package 'compare'.
你也可以考虑包'比较'。
library(compare)
compareEqual(a,b)#TRUE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.