Hi R user, I am new to R, and I have a very simple question for you. I have two matrix A and B, with internally redundant rownames (but variables are different). Some, but not all the rownames are shared among the two matrix. I want to create a greater matrix that combines the previuos two, and has all the possible combinations of matching rownames lines among matrix A and B. looking for the solution I bumped in merge but actually works on data.frame, and in dataframe there could be no redundancy in names. can you help me?? [[alternative HTML version deleted]]
let say something like this>a=matrix(1:25, nrow=5)>rownames(a)=letters[1:5] > colnames(a)=rep("A", 5)> aA A A A A a 1 6 11 16 21 b 2 7 12 17 22 c 3 8 13 18 23 d 4 9 14 19 24 e 5 10 15 20 25> b=matrix(1:40, nrow=8) > rownames(b)=c(rep("a",4),rep("b",4)) > colnames(b)=rep("B", 5)> bB B B B B a 1 9 17 25 33 a 2 10 18 26 34 a 3 11 19 27 35 a 4 12 20 28 36 b 5 13 21 29 37 b 6 14 22 30 38 b 7 15 23 31 39 b 8 16 24 32 40 as a results I wold like something like A A A A A B B B B B a 1 6 11 16 21 1 9 17 25 33 a 1 6 11 16 21 2 10 18 26 34 a 1 6 11 16 21 3 11 19 27 35 a 1 6 11 16 21 4 12 20 28 36 b 2 7 12 17 22 5 13 21 29 37 b 2 7 12 17 22 6 14 22 30 38 b 2 7 12 17 22 7 15 23 31 39 b 2 7 12 17 22 8 16 24 32 40 does it is clear? is there a function that automate this operation? thank you very much! On 8/16/07, jim holtman <jholtman@gmail.com> wrote:> > Can you provide an example of what you mean; e.g., the two input > matrices and the desired output. > > On 8/16/07, Gianni Burgin <gianni.burgin@gmail.com> wrote: > > Hi R user, > > > > I am new to R, and I have a very simple question for you. I have two > matrix > > A and B, with internally redundant rownames (but variables are > different). > > Some, but not all the rownames are shared among the two matrix. I want > to > > create a greater matrix that combines the previuos two, and has all the > > possible combinations of matching rownames lines among matrix A and B. > > > > looking for the solution I bumped in merge but actually works on > data.frame, > > and in dataframe there could be no redundancy in names. > > > > > > can you help me?? > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > 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. > > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem you are trying to solve? >[[alternative HTML version deleted]]
[Gianni Burgin]>let say something like this>>a=matrix(1:25, nrow=5)>>rownames(a)=letters[1:5] >> colnames(a)=rep("A", 5)>> a > A A A A A >a 1 6 11 16 21 >b 2 7 12 17 22 >c 3 8 13 18 23 >d 4 9 14 19 24 >e 5 10 15 20 25>> b=matrix(1:40, nrow=8) >> rownames(b)=c(rep("a",4),rep("b",4)) >> colnames(b)=rep("B", 5)>> b > B B B B B >a 1 9 17 25 33 >a 2 10 18 26 34 >a 3 11 19 27 35 >a 4 12 20 28 36 >b 5 13 21 29 37 >b 6 14 22 30 38 >b 7 15 23 31 39 >b 8 16 24 32 40>as a results I wold like something like> A A A A A B B B B B >a 1 6 11 16 21 1 9 17 25 33 >a 1 6 11 16 21 2 10 18 26 34 >a 1 6 11 16 21 3 11 19 27 35 >a 1 6 11 16 21 4 12 20 28 36 >b 2 7 12 17 22 5 13 21 29 37 >b 2 7 12 17 22 6 14 22 30 38 >b 2 7 12 17 22 7 15 23 31 39 >b 2 7 12 17 22 8 16 24 32 40>does it is clear? is there a function that automate this operation?Like, maybe: cbind(a[rownames(b),], b) -- Fran?ois Pinard http://pinard.progiciels-bpi.ca
This does not work in the general case. To see this, try: rownames(a}[5] <- "a" and see what happens then. --- Fran?ois Pinard <pinard at iro.umontreal.ca> wrote:> [Gianni Burgin] > >let say something like this > > >>a=matrix(1:25, nrow=5) > > >>rownames(a)=letters[1:5] > >> colnames(a)=rep("A", 5) > > >> a > > A A A A A > >a 1 6 11 16 21 > >b 2 7 12 17 22 > >c 3 8 13 18 23 > >d 4 9 14 19 24 > >e 5 10 15 20 25 > > >> b=matrix(1:40, nrow=8) > >> rownames(b)=c(rep("a",4),rep("b",4)) > >> colnames(b)=rep("B", 5) > > >> b > > B B B B B > >a 1 9 17 25 33 > >a 2 10 18 26 34 > >a 3 11 19 27 35 > >a 4 12 20 28 36 > >b 5 13 21 29 37 > >b 6 14 22 30 38 > >b 7 15 23 31 39 > >b 8 16 24 32 40 > > >as a results I wold like something like > > > A A A A A B B B B B > >a 1 6 11 16 21 1 9 17 25 33 > >a 1 6 11 16 21 2 10 18 26 34 > >a 1 6 11 16 21 3 11 19 27 35 > >a 1 6 11 16 21 4 12 20 28 36 > >b 2 7 12 17 22 5 13 21 29 37 > >b 2 7 12 17 22 6 14 22 30 38 > >b 2 7 12 17 22 7 15 23 31 39 > >b 2 7 12 17 22 8 16 24 32 40 > > >does it is clear? is there a function that automate > this operation? > > Like, maybe: > > cbind(a[rownames(b),], b) > > > > -- > Fran?ois Pinard http://pinard.progiciels-bpi.ca > > ______________________________________________ > 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. >
Try this. We convert to data frame placing the row names in column 1, do the merge, remove column 1 and convert back to matrix: # test input a <- matrix(1:25, nrow = 5, dimnames = list(letters[1:5], rep("A", 5))) b <- matrix(1:40, nrow = 8, dimnames = list(rep(letters[1:2], each = 4), rep("B", 5))) # 1. process to.DF <- function(x) data.frame(rn = row.names(x), x, row.names = 1:nrow(x)) out <- as.matrix(merge(to.DF(a), to.DF(b), by = 1)[,-1]) colnames(out) <- c(colnames(a), colnames(b)) out # 2. same but merge is done using sqldf # assume same a, b and to.DF as before library(sqldf) DFa <- to.DF(a) DFb <- to.DF(b) out <- as.matrix(sqldf("select * from DFa join DFb using(rn)")[-1]) colnames(out) <- c(colnames(a), colnames(b)) out # 3. same but uses sqldf and proto (which sqldf automatically loads) # assume same a, b and to.DF as before library(sqldf) out <- as.matrix(sqldf("select * from a join b using(rn)", envir = proto(a = to.DF(a), b = to.DF(b)))[-1]) colnames(out) <- c(colnames(a), colnames(b)) out On 8/16/07, Gianni Burgin <gianni.burgin at gmail.com> wrote:> let say something like this > > > >a=matrix(1:25, nrow=5) > > >rownames(a)=letters[1:5] > > colnames(a)=rep("A", 5) > > > a > A A A A A > a 1 6 11 16 21 > b 2 7 12 17 22 > c 3 8 13 18 23 > d 4 9 14 19 24 > e 5 10 15 20 25 > > > b=matrix(1:40, nrow=8) > > rownames(b)=c(rep("a",4),rep("b",4)) > > colnames(b)=rep("B", 5) > > > b > B B B B B > a 1 9 17 25 33 > a 2 10 18 26 34 > a 3 11 19 27 35 > a 4 12 20 28 36 > b 5 13 21 29 37 > b 6 14 22 30 38 > b 7 15 23 31 39 > b 8 16 24 32 40 > > as a results I wold like something like > > A A A A A B B B B B > a 1 6 11 16 21 1 9 17 25 33 > a 1 6 11 16 21 2 10 18 26 34 > a 1 6 11 16 21 3 11 19 27 35 > a 1 6 11 16 21 4 12 20 28 36 > b 2 7 12 17 22 5 13 21 29 37 > b 2 7 12 17 22 6 14 22 30 38 > b 2 7 12 17 22 7 15 23 31 39 > b 2 7 12 17 22 8 16 24 32 40 > > > does it is clear? is there a function that automate this operation? > > > thank you very much! > > > > > On 8/16/07, jim holtman <jholtman at gmail.com> wrote: > > > > Can you provide an example of what you mean; e.g., the two input > > matrices and the desired output. > > > > On 8/16/07, Gianni Burgin <gianni.burgin at gmail.com> wrote: > > > Hi R user, > > > > > > I am new to R, and I have a very simple question for you. I have two > > matrix > > > A and B, with internally redundant rownames (but variables are > > different). > > > Some, but not all the rownames are shared among the two matrix. I want > > to > > > create a greater matrix that combines the previuos two, and has all the > > > possible combinations of matching rownames lines among matrix A and B. > > > > > > looking for the solution I bumped in merge but actually works on > > data.frame, > > > and in dataframe there could be no redundancy in names. > > > > > > > > > can you help me?? > > > > > > [[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? > > > > [[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. >
Maybe Matching Threads
- Help with lattice graphics
- Q: combine 2 data frames with missing values
- how to apply the dummy coding rule in a dataframe with complete factor levels to another dataframe with incomplete factor levels?
- binary symmetric matrix combination
- problem with merge