测试R中两个向量之间的匹配和顺序 [英] Test Match and Order between two vectors in R

查看:385
本文介绍了测试R中两个向量之间的匹配和顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试两个向量之间的匹配和顺序.我知道比赛功能;是否有覆盖层可以同时评估订单?例如:

I would like to test the match and order between two vectors. I'm aware of the match function; are there overlays to assess the order simultaneously? For example:

x <- c("a", "b", "c")
y <- c("b", "a", "c")   
x %in% y    

存在完美匹配,但顺序不正确.关于如何识别的想法?谢谢.

There are perfect matches, but the ordering is not correct. Thoughts on how to identify that? Thanks.

推荐答案

test_match_order <- function(x,y) {

if (all(x==y)) print('Perfect match in same order')

if (!all(x==y) && all(sort(x)==sort(y))) print('Perfect match in wrong order')

if (!all(x==y) && !all(sort(x)==sort(y))) print('No match')
}

test_match_order(x,y)

[1] "Perfect match in wrong order"

这是另一个基于我上面原始注释的版本,它对下面的@alexis_laz进行了改进,使该功能更强大:

And here is another version based on my original comment above with an improvement from @alexis_laz below that makes the function more robust:

test_match_order2 <- function(x,y) {

if (isTRUE(all.equal(x,y))) print('Perfect match in same order')

if (!isTRUE(all.equal(x,y)) && isTRUE(all.equal(sort(x),sort(y)))) print('Perfect match in wrong order')

if (!isTRUE(all.equal(x,y)) && !isTRUE(all.equal(sort(x),sort(y)))) print('No match')
}

这篇关于测试R中两个向量之间的匹配和顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆