成对的独特组合忽略方向

编程入门 行业动态 更新时间:2024-10-24 08:19:31
本文介绍了成对的独特组合忽略方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想根据R中的一个共同的字符在一个数据框架中聚合某个值。问题是我对成对组合的不同方向不感兴趣。 所以例如

d = data.frame(x = LETTERS [1:5],y = LETTERS [ 5:1])$ ​​b $ bxy 1 AE 2 BD 3 CC 4 DB 5 EA

然后按以下计算结果:

d $ z< - paste0(d $ x,d $ y,sep =_)

问题是我对成对差异不感兴趣。所以在这个简单例子中, A_E 应该与 E_A 相同。

是否有一个简单的解决方案来粘贴它们?我正在考虑在将它们组合成一个向量之前对它们进行排序。

解决方案

一个选项是使用 pmin 和 pmax :

(d,z = paste(pmin(x,y),pmax(x,y),sep =_))#xyz #1 AE A_E #2 BD B_D #3 CC C_C #4 DB B_D #5 EA A_E

请注意,如果它们是因子,则可能需要将x和y转换为字符。

d< - data.frame(x = LETTERS [1:5],y = LETTERS [5:1],stringsAsFactors = FALSE)

I want to aggregate a certain value in a data.frame based on a common character in R. The Problem is that I am not interested in different directions of the pairwise combination. So for instance

d = data.frame( x = LETTERS[1:5], y = LETTERS[5:1] ) x y 1 A E 2 B D 3 C C 4 D B 5 E A

The combination would be then calculated like this:

d$z <- paste0(d$x,d$y,sep="_")

The problem is that i am not interested in pairwise differences. So A_E should be the same as E_A in this simple example.

Is there a clever short solution to paste them? I am currently thinking about sorting each one before combining them into a vector.

解决方案

One option is to use pmin and pmax:

transform(d, z = paste(pmin(x,y), pmax(x,y), sep="_")) # x y z #1 A E A_E #2 B D B_D #3 C C C_C #4 D B B_D #5 E A A_E

Note that you might need to convert x and y to character if they are factors.

d <- data.frame( x = LETTERS[1:5], y = LETTERS[5:1], stringsAsFactors = FALSE)

更多推荐

成对的独特组合忽略方向

本文发布于:2023-11-29 22:58:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   独特   方向

发布评论

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

>www.elefans.com

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