Here is an example to explain what I want to do. I have a data frame like:
X Y
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
I want to change it to another format:
X1 Y1 X2 Y2
1 1 1 1
1 2 2 1
1 3 3 1
......
For two rows in the first table, say X=1, Y=2 and X=2, Y=1. They just exchange each other's values. So I want to put such rows in on row, as shown in the second table, and then remove the duplicates. So, the 'thin and long' table is turned to 'short and fat'. I know how to do it using two for loops. But in R, such operation takes for ever. So, can anyone help me with a quick way?
Here is a smallest example:
The original table is:
X Y
1 2
2 1
The transferred table that I want is like:
X1 Y1 X2 Y2
1 2 2 1
So, the rows in the first table that just exchanges values are integrated into one row in the second table and the extra row in the first table is removed.