在两个无序字符向量之间执行非成对全对全比较 --- 相交的反面 --- 全对全 setdiff [英] Perform non-pairwise all-to-all comparisons between two unordered character vectors --- The opposite of intersect --- all-to-all setdiff

查看:21
本文介绍了在两个无序字符向量之间执行非成对全对全比较 --- 相交的反面 --- 全对全 setdiff的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例数据

v1 <- c("E82391", "X2329323", "C239923", "E1211", "N23932", "F93249232", "X93201", "X9023111", "O92311", "9000F", "K9232932", "L9232932", "X02311111")
v2 <- c("L9232932", "C239923", "E1211", "E82391", "F93249232", "U82832")

问题

我只想提取那些在一个向量中而不是另一个向量中的项目.

I want to extract only those items that are in one of the vectors and not in the other.

我了解 setdiff 无法比较两个无序字符向量并找出两者之间的所有差异..

I understand that setdiff is unable to compare two unordered character vectors and find all the differences between the two..

例如,%in% 是否在两个字符向量之间执行全对全比较?

Does, for example, %in% perform all-to-all comparisons between two character vectors?

在这种情况下,它确实有效(尽管它不会报告那些在 v2 中而不在 v1 中的元素).

In this case, it does work (although it does not report those elements that are in v2 and not in v1).

> v1[!v1 %in% v2]
[1] "X2329323"  "N23932"    "X93201"    "X9023111"  "O92311"    "9000F"     "K9232932"  "X02311111"

另一种方法是使用名为 outersect 的用户定义函数作为 此处显示 显示了所有差异.

Another way is using a user-defined function named outersect as shown here which shows all the differences.

outersect <- function(x, y) {
  sort(c(x[!x%in%y],
         y[!y%in%x]))
}

outersect(v1,v2)

问题

我真的很想知道是否有任何 R 函数可以轻松地在两个字符向量之间执行全面比较!这个想法是为了真正提高代码的可读性(特别是当有几十个向量需要相互比较时).

I am really interested to know whether there are any R functions that would easily perform all-to-all comparisons between two character vectors! The idea is to really improve the readability of the code (specially when there are dozens of vectors that need to be compared to each other).

执行这种全方位比较的最安全、最有效的方法是什么?更具体地说,R 中是否有一个函数可以

What is the safest and most efficient way to perform such all-to-all comparisons? More specifically, is there a function in R that would

参考资料.

  1. 布雷亚尔,托尼.outersect():R 的 intersect() 函数的反面",2011 年 11 月.R-bloggers.
  1. Breyal, Tony. "outersect(): The opposite of R’s intersect() function", Nov. 2011. R-bloggers.

推荐答案

也许这个:

both <- c(unique(v1),unique(v2))
both[! (duplicated(both) | duplicated(both, fromLast = T))]
[1] "X2329323"  "N23932"    "X93201"    "X9023111"  "O92311"    "9000F"     "K9232932"  "X02311111" "U82832"   

这篇关于在两个无序字符向量之间执行非成对全对全比较 --- 相交的反面 --- 全对全 setdiff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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