测试R中两个向量之间的匹配和顺序

编程入门 行业动态 更新时间:2024-10-18 12:32:10
本文介绍了测试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中两个向量之间的匹配和顺序

本文发布于:2023-06-03 12:19:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   顺序   两个   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!