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! ______________________________________________ R-help@r-project.org 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.