Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
854 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...