Federico Abascal
2007-Jan-30 11:27 UTC
[R] how to join two arrays using their column names intersection
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")> ar1A B D E [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16> ar2C 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
Federico Abascal
2007-Jan-30 11:56 UTC
[R] how to join two arrays using their column names intersection
I have found something that partially works. Just to illustrate what am I looking for: for(a in colnames(ar1)) {if(a %in% colnames(ar2)) { cat(a, "\t", ar1[,a],"\t",ar2[,a],"\n")}} A 1 2 3 4 5 6 7 8 B 5 6 7 8 13 14 15 16 E 13 14 15 16 9 10 11 12 I think I can use something similar to obtain the desired results, but I am curious to know if there is a better way to do it. Federico Federico Abascal wrote:> 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. > >[[alternative HTML version deleted]]
Vladimir Eremeev
2007-Jan-30 12:01 UTC
[R] how to join two arrays using their column names intersection
dfr1<-merge(ar1,ar2,all=TRUE) result<-as.matrix(dfr1[apply(dfr1,2,function(x)!any(is.na(x)))]) Federico Abascal wrote:> > 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 at 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. > >-- View this message in context: http://www.nabble.com/-R--how-to-join-two-arrays-using-their-column-names-intersection-tf3141828.html#a8708210 Sent from the R help mailing list archive at Nabble.com.
see ?order ar2[,order(colnames(ar2))] Federico Abascal wrote:> > I have another question: how can I sort the columns of an array > according to its column names (for ar2, change CAEB to ABCE)? >-- View this message in context: http://www.nabble.com/-R--how-to-join-two-arrays-using-their-column-names-intersection-tf3141828.html#a8708259 Sent from the R help mailing list archive at Nabble.com.
jim holtman
2007-Jan-30 12:30 UTC
[R] how to join two arrays using their column names intersection
> 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") > > ar1A B D E [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16> ar2C A E B [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16> # get the common names between the matrices > same <- intersect(colnames(ar1), colnames(ar2)) > same[1] "A" "B" "E"> # join them together > rbind(ar1[,same], ar2[,same])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>On 1/30/07, Federico Abascal <fabascal at cnb.uam.es> wrote:> I have found something that partially works. Just to illustrate what am > I looking for: > > for(a in colnames(ar1)) {if(a %in% colnames(ar2)) { cat(a, "\t", > ar1[,a],"\t",ar2[,a],"\n")}} > A 1 2 3 4 5 6 7 8 > B 5 6 7 8 13 14 15 16 > E 13 14 15 16 9 10 11 12 > > I think I can use something similar to obtain the desired results, but I > am curious to know if there is a better way to do it. > Federico > > > Federico Abascal wrote: > > 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 at 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. > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
BXC (Bendix Carstensen)
2007-Jan-31 12:18 UTC
[R] how to join two arrays using their column names intersection
Here is a workable solution: df1 <- data.frame(ar1) df2 <- data.frame(ar2) cmn <- intersect(names(df1),names(df2)) rbind(df1[,cmn],df2[,cmn]) Best Bendix ______________________________________________ Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2-4 DK-2820 Gentofte Denmark +45 44 43 87 38 (direct) +45 30 75 87 38 (mobile) +45 44 43 73 13 (fax) bxc at steno.dk http://www.biostat.ku.dk/~bxc ______________________________________________> -----Original Message----- > From: Federico Abascal [mailto:fabascal at cnb.uam.es] > Sent: Tuesday, January 30, 2007 12:28 PM > To: r-help at stat.math.ethz.ch > Subject: [R] how to join two arrays using their column names > intersection > > 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 > > >