[R] binary symmetric matrix combination

arun smartpink111 at yahoo.com
Fri Sep 20 14:59:03 CEST 2013


Hi Elio,
Try this:

library(stringr)

lines1<-str_trim(gsub("\t"," ",readLines("elio.txt")))
 lst1<-lapply(split(lines1,cumsum(lines1=="")),function(x) x[x!=""])

lst2<- lapply(lst1[lapply(lst1,length)>0],function(x) as.matrix(read.table(text=x,row.names=1)))
names(lst2)<- paste0("m",seq_along(lst2))
dat<- do.call(rbind,lapply(names(lst2),function(x) {x1<- lst2[[x]]; cbind(expand.grid(rep(list(colnames(x1)),2),stringsAsFactors=FALSE),value=as.vector(x1))}))
library(reshape2)
res<- dcast(dat,Var1~Var2,value.var="value",sum)
 row.names(res)<- res[,1]
 res<- as.matrix(res[,-1])
 dim(res)
#[1] 14 14


A.K.



________________________________
From: Elio Shijaku <selius at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Sent: Friday, September 20, 2013 3:38 AM
Subject: Re: binary symmetric matrix combination



Hi Arun,

Let me explain my point. With your great help, I was able to create matrices such as m1, m2, etc. My initial objective was to combine the binary matrices and I succeeded in that again with your help. The only problem I have left is that since  I have about 350 matrices to combine for a single giant symmetric binary matrix of 470x470 size and I have to repeat this 22 times (got 22 giant matrices to build), I was hoping to not have to manually input each matrix but to use your list2 command to get them all from Excel. Now the problem is how to get the elements from list2 to then apply the commands:


Out1<-
as.matrix(read.table(text="y1 g24 c1 c2 l17 h4 s2 s30 e5
l15",sep="",header=TRUE))
names1<-unique(c(colnames(m1),colnames(m2),colnames(m3),colnames(m4),colnames(m5))) 
Out3<-matrix(0,length(names1),length(names1),dimnames=list(names1,names1)) 
vecOut<-paste0(colnames(Out3)[col(Out3)],rownames(Out3)[row(Out3)]) lst1<-sapply(paste0("m",1:5),function(x)
{x1<- get(x); x2<-paste0(colnames(x1)[col(x1)],rownames(x1)[row(x1)]);
match(x2,vecOut)}) 
lst2<- list(m1,m2,m3,m4,m5) 
N<- length(lst1) 

 fn1<- function(N,Out){ 
 i=1 
 while(i<=N){ 
 Out[lst1[[i]]]<-lst2[[i]] 
 i<-i+1 
 } 
Out 
 } 
fn1(N,Out3)

Thanks a lot!!

Best,

Elio



More information about the R-help mailing list