我想根据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 AThe 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_ENote 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)
更多推荐
成对的独特组合忽略方向
发布评论