Hi, May be this helps: m1<- as.matrix(read.table(text=" y1 g24 y1 0 1 g24 1 0 ",sep="",header=TRUE)) m2<-as.matrix(read.table(text="y1 c1 c2 l17 ?y1 0 1 1 1 ?c1 1 0 1 1 ?c2 1 1 0 1 ?l17 1 1 1 0",sep="",header=TRUE)) m3<- as.matrix(read.table(text="y1 h4??? s2???? s30 ?y1 0 1 1 1 ?h4 1 0 1 1 ?s2 1 1 0 1 ?s30 1 1 1 0",sep="",header=TRUE)) m4<- as.matrix(read.table(text="y1 e5 l15 ?y1 0 1 1 e5 1 0 1 l15 1 1 0",sep="",header=TRUE)) ###desired output: at some place the label is "s2" and at other "s29".? I used "s2" for consistency Out1<- as.matrix(read.table(text="y1 g24 c1 c2 l17 h4 s2 s30 e5 l15 y1 0 1 1 1 1 1 1 1 1 1 g24 1 0 0 0 0 0 0 0 0 0 c1 1 0 0 1 1 0 0 0 0 0 c2 1 0 1 0 1 0 0 0 0 0 l17 1 0 1 1 0 0 0 0 0 0 h4 1 0 0 0 0 0 1 1 0 0 s2 1 0 0 0 0 1 0 1 0 0 s30 1 0 0 0 0 1 1 0 0 0 e5 1 0 0 0 0 0 0 0 0 1 l15 1 0 0 0 0 0 0 0 1 0",sep="",header=TRUE)) names1<-unique(c(colnames(m1),colnames(m2),colnames(m3),colnames(m4))) Out2<-matrix(0,length(names1),length(names1),dimnames=list(names1,names1)) vec1<- paste0(colnames(m1)[col(m1)],rownames(m1)[row(m1)]) vecOut<- paste0(colnames(Out2)[col(Out2)],rownames(Out2)[row(Out2)]) Out2[match(vec1,vecOut)]<- m1 vec2<- paste0(colnames(m2)[col(m2)],rownames(m2)[row(m2)]) Out2[match(vec2,vecOut)]<- m2 vec3<- paste0(colnames(m3)[col(m3)],rownames(m3)[row(m3)]) Out2[match(vec3,vecOut)]<- m3 vec4<- paste0(colnames(m4)[col(m4)],rownames(m4)[row(m4)]) Out2[match(vec4,vecOut)]<- m4 ?all.equal(Out1,Out2) #[1] TRUE ?Out2 ??? y1 g24 c1 c2 l17 h4 s2 s30 e5 l15 y1?? 0?? 1? 1? 1?? 1? 1? 1?? 1? 1?? 1 g24? 1?? 0? 0? 0?? 0? 0? 0?? 0? 0?? 0 c1?? 1?? 0? 0? 1?? 1? 0? 0?? 0? 0?? 0 c2?? 1?? 0? 1? 0?? 1? 0? 0?? 0? 0?? 0 l17? 1?? 0? 1? 1?? 0? 0? 0?? 0? 0?? 0 h4?? 1?? 0? 0? 0?? 0? 0? 1?? 1? 0?? 0 s2?? 1?? 0? 0? 0?? 0? 1? 0?? 1? 0?? 0 s30? 1?? 0? 0? 0?? 0? 1? 1?? 0? 0?? 0 e5?? 1?? 0? 0? 0?? 0? 0? 0?? 0? 0?? 1 l15? 1?? 0? 0? 0?? 0? 0? 0?? 0? 1?? 0 A.K. I have the following binary labeled matrices with different dimensions (2x2, 3x3, 4x4) which I need to create in R as seen below: ? ? ? ? y1 g24 y1 0 1 g2 4 1 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?y1 c1 c2 l17 ?y1 0 1 1 1 ?c1 1 0 1 1 ?c2 1 1 0 1 ?l17 1 1 1 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?y1 h4 ? ?s2 ? ? s30 ?y1 0 1 1 1 ?h4 1 0 1 1 ?s29 1 1 0 1 ?s30 1 1 1 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? y1 e5 l15 ?y1 0 1 1 e5 1 0 1 l15 1 1 0 Then, I need to combine them to achieve the following result: ? ? ? ? y1 g24 c1 c2 l17 h4 s29 s30 e5 l15 y1 0 1 1 1 1 1 1 1 1 1 g24 1 0 0 0 0 0 0 0 0 0 c1 1 0 0 1 1 0 0 0 0 0 c2 1 0 1 0 1 0 0 0 0 0 l17 1 0 1 1 0 0 0 0 0 0 h4 1 0 0 0 0 0 1 1 0 0 s29 1 0 0 0 0 1 0 1 0 0 s30 1 0 0 0 0 1 1 0 0 0 e5 1 0 0 0 0 0 0 0 0 1 l15 1 0 0 0 0 0 0 0 1 0 Your help would be very much appreciated. ps. if the matrices don't appear correctly, please notice that all values different from 0 and 1 are row and column names Thank You!
Also, some of the steps could be reduced by: names1<-unique(c(colnames(m1),colnames(m2),colnames(m3),colnames(m4))) Out3<-matrix(0,length(names1),length(names1),dimnames=list(names1,names1)) lst1<-sapply(paste0("m",1:4),function(x) {x1<- get(x); x2<-paste0(colnames(x1)[col(x1)],rownames(x1)[row(x1)]); match(x2,vecOut)}) lst2<- list(m1,m2,m3,m4) N<- length(lst1) ?fn1<- function(N,Out){ ?i=1 ?while(i<=N){ ?Out[lst1[[i]]]<-lst2[[i]] ?i<-i+1 ?} Out ?} fn1(N,Out3) #??? y1 g24 c1 c2 l17 h4 s2 s30 e5 l15 #y1?? 0?? 1? 1? 1?? 1? 1? 1?? 1? 1?? 1 #g24? 1?? 0? 0? 0?? 0? 0? 0?? 0? 0?? 0 #c1?? 1?? 0? 0? 1?? 1? 0? 0?? 0? 0?? 0 #c2?? 1?? 0? 1? 0?? 1? 0? 0?? 0? 0?? 0 #l17? 1?? 0? 1? 1?? 0? 0? 0?? 0? 0?? 0 #h4?? 1?? 0? 0? 0?? 0? 0? 1?? 1? 0?? 0 #s2?? 1?? 0? 0? 0?? 0? 1? 0?? 1? 0?? 0 #s30? 1?? 0? 0? 0?? 0? 1? 1?? 0? 0?? 0 #e5?? 1?? 0? 0? 0?? 0? 0? 0?? 0? 0?? 1 #l15? 1?? 0? 0? 0?? 0? 0? 0?? 0? 1?? 0 ?identical(Out2,fn1(N,Out3)) #[1] TRUE A.K. ----- Original Message ----- From: arun <smartpink111 at yahoo.com> To: R help <r-help at r-project.org> Cc: Sent: Thursday, September 5, 2013 4:09 PM Subject: Re: binary symmetric matrix combination Hi, May be this helps: m1<- as.matrix(read.table(text=" y1 g24 y1 0 1 g24 1 0 ",sep="",header=TRUE)) m2<-as.matrix(read.table(text="y1 c1 c2 l17 ?y1 0 1 1 1 ?c1 1 0 1 1 ?c2 1 1 0 1 ?l17 1 1 1 0",sep="",header=TRUE)) m3<- as.matrix(read.table(text="y1 h4??? s2???? s30 ?y1 0 1 1 1 ?h4 1 0 1 1 ?s2 1 1 0 1 ?s30 1 1 1 0",sep="",header=TRUE)) m4<- as.matrix(read.table(text="y1 e5 l15 ?y1 0 1 1 e5 1 0 1 l15 1 1 0",sep="",header=TRUE)) ###desired output: at some place the label is "s2" and at other "s29".? I used "s2" for consistency Out1<- as.matrix(read.table(text="y1 g24 c1 c2 l17 h4 s2 s30 e5 l15 y1 0 1 1 1 1 1 1 1 1 1 g24 1 0 0 0 0 0 0 0 0 0 c1 1 0 0 1 1 0 0 0 0 0 c2 1 0 1 0 1 0 0 0 0 0 l17 1 0 1 1 0 0 0 0 0 0 h4 1 0 0 0 0 0 1 1 0 0 s2 1 0 0 0 0 1 0 1 0 0 s30 1 0 0 0 0 1 1 0 0 0 e5 1 0 0 0 0 0 0 0 0 1 l15 1 0 0 0 0 0 0 0 1 0",sep="",header=TRUE)) names1<-unique(c(colnames(m1),colnames(m2),colnames(m3),colnames(m4))) Out2<-matrix(0,length(names1),length(names1),dimnames=list(names1,names1)) vec1<- paste0(colnames(m1)[col(m1)],rownames(m1)[row(m1)]) vecOut<- paste0(colnames(Out2)[col(Out2)],rownames(Out2)[row(Out2)]) Out2[match(vec1,vecOut)]<- m1 vec2<- paste0(colnames(m2)[col(m2)],rownames(m2)[row(m2)]) Out2[match(vec2,vecOut)]<- m2 vec3<- paste0(colnames(m3)[col(m3)],rownames(m3)[row(m3)]) Out2[match(vec3,vecOut)]<- m3 vec4<- paste0(colnames(m4)[col(m4)],rownames(m4)[row(m4)]) Out2[match(vec4,vecOut)]<- m4 ?all.equal(Out1,Out2) #[1] TRUE ?Out2 ??? y1 g24 c1 c2 l17 h4 s2 s30 e5 l15 y1?? 0?? 1? 1? 1?? 1? 1? 1?? 1? 1?? 1 g24? 1?? 0? 0? 0?? 0? 0? 0?? 0? 0?? 0 c1?? 1?? 0? 0? 1?? 1? 0? 0?? 0? 0?? 0 c2?? 1?? 0? 1? 0?? 1? 0? 0?? 0? 0?? 0 l17? 1?? 0? 1? 1?? 0? 0? 0?? 0? 0?? 0 h4?? 1?? 0? 0? 0?? 0? 0? 1?? 1? 0?? 0 s2?? 1?? 0? 0? 0?? 0? 1? 0?? 1? 0?? 0 s30? 1?? 0? 0? 0?? 0? 1? 1?? 0? 0?? 0 e5?? 1?? 0? 0? 0?? 0? 0? 0?? 0? 0?? 1 l15? 1?? 0? 0? 0?? 0? 0? 0?? 0? 1?? 0 A.K. I have the following binary labeled matrices with different dimensions (2x2, 3x3, 4x4) which I need to create in R as seen below: ? ? ? ? y1??? g24 y1 ??? 0??? 1 g2 4??? 1??? 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?y1??? c1??? c2??? l17 ?y1??? 0??? 1??? 1??? 1 ?c1??? 1??? 0??? 1??? 1 ?c2??? 1??? 1??? 0??? 1 ?l17??? 1??? 1??? 1??? 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?y1??? h4 ? ?s2 ? ? s30 ?y1??? 0??? 1??? 1??? 1 ?h4??? 1??? 0??? 1??? 1 ?s29??? 1??? 1??? 0??? 1 ?s30??? 1??? 1??? 1??? 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? y1 ??? e5??? l15 ?y1??? 0??? 1??? 1 e5??? 1??? 0??? 1 l15??? 1??? 1??? 0 Then, I need to combine them to achieve the following result: ? ? ? ? y1??? g24??? c1??? c2??? l17??? h4??? s29??? s30??? e5??? l15 y1??? 0??? 1??? 1??? 1??? 1??? 1??? 1??? 1??? 1??? 1 g24??? 1??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 0 c1??? 1??? 0??? 0??? 1??? 1??? 0??? 0??? 0??? 0??? 0 c2??? 1??? 0??? 1??? 0??? 1??? 0??? 0??? 0??? 0??? 0 l17??? 1??? 0??? 1??? 1??? 0??? 0??? 0??? 0??? 0??? 0 h4??? 1??? 0??? 0??? 0??? 0??? 0??? 1??? 1??? 0??? 0 s29??? 1??? 0??? 0??? 0??? 0??? 1??? 0??? 1??? 0??? 0 s30??? 1??? 0??? 0??? 0??? 0??? 1??? 1??? 0??? 0??? 0 e5??? 1??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 1 l15??? 1??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 1??? 0 Your help would be very much appreciated. ps. if the matrices don't appear correctly, please notice that all values different from 0 and 1 are row and column names Thank You!
Hi, I have another question related to the same problem. I have a text file with about 350 matrices each separated by a blank row. My question is how to make R believe each matrix is separate and has a specific name m1, m2,....m350. Below is an example: aa5 aa10 b253 b254 aa5 0 1 1 1 aa10 1 0 1 1 b253 1 1 0 1 b254 1 1 1 0 aa5 aa9 b27 b29 aa5 0 1 1 1 aa9 1 0 1 1 b27 1 1 0 1 b29 1 1 1 0 a15 b3 g23 i250 a15 0 1 1 1 b3 1 0 1 1 g23 1 1 0 1 i250 1 1 1 0 a15 a16 q27 v87 a15 0 1 1 1 a16 1 0 1 1 q27 1 1 0 1 v87 1 1 1 0 This is what I get from R, how to make it believe each one is a matrix and name the matrix m1, m2, m3 and so on. Thanks a lot!! On Sun, Sep 8, 2013 at 12:44 PM, supernovartis [via R] < ml-node+s789695n4675624h21@n4.nabble.com> wrote:> Once again very many thanks for such a great support. Will report back in > case of further issues. > > Best regards, > Elio > > ------------------------------ > If you reply to this email, your message will be added to the discussion > below: > > http://r.789695.n4.nabble.com/binary-symmetric-matrix-combination-tp4675440p4675624.html > To unsubscribe from binary symmetric matrix combination, click here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4675440&code=c2VsaXVzQGdtYWlsLmNvbXw0Njc1NDQwfDMwOTY1Njg2Mg==> > . > NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> >-- View this message in context: http://r.789695.n4.nabble.com/binary-symmetric-matrix-combination-tp4675440p4676427.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]