[R] how to join two arrays using their column names intersection

From: Federico Abascal <fabascal_at_cnb.uam.es>
Date: Tue 30 Jan 2007 - 11:27:53 GMT


Dear all,

I have a problem that may be someone of you can help. I am a newbie and do not find how to do it in manuals.

I have two arrays, for example:

ar1 <- array(data=c(1:16),dim=c(4,4))
ar2 <- array(data=c(1:16),dim=c(4,4))
colnames(ar1)<-c("A","B","D","E")
colnames(ar2)<-c("C","A","E","B")

> ar1

     A B D E
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16

> ar2

     C A E B
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16

I would like to join both arrays only for the columns present in both ar1 and ar2 (the intersection). I would like to obtain this:

     A B E
[1,] 1 5 13
[2,] 2 6 14
[3,] 3 7 15
[4,] 4 8 16
[5,] 5 13 9
[6,] 6 14 10
[7,] 7 15 11
[8,] 8 16 12

(rows 5-8 correspond to ar2)

I have tried several things but I am unable to accomplish it. Any experienced user could give me some advice, please?

I have another question: how can I sort the columns of an array according to its column names (for ar2, change CAEB to ABCE)?

Thanks in advance!
Federico



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue Jan 30 22:31:05 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 31 Jan 2007 - 12:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.