Hi Elio, Try this: library(stringr) ?lines1<-str_trim(gsub("\t"," ",readLines("elio.txt"))) ?lst1<-lapply(split(lines1,cumsum(lines2=="")),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)) lst2[1:2] #$m1 ?# ?? aa5 aa10 b253 b254 #aa5??? 0??? 1??? 1??? 1 #aa10?? 1??? 0??? 1??? 1 #b253?? 1??? 1??? 0??? 1 #b254?? 1??? 1??? 1??? 0 # #$m2 ?# ? aa5 aa9 b27 b29 #aa5?? 0?? 1?? 1?? 1 #aa9?? 1?? 0?? 1?? 1 #b27?? 1?? 1?? 0?? 1 #b29?? 1?? 1?? 1?? 0 A.K. ________________________________ From: Elio Shijaku <selius at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Thursday, September 19, 2013 3:27 AM Subject: Re: binary symmetric matrix combination Hi Arun, Please find attached the text file. Let me know if anything else is needed. Thanks a lot for your continuous help. Best, Elio
Hi Arun, Worked perfectly, one last question how to read the matrices which have the dollar sign? Thanks again. On Thu, Sep 19, 2013 at 3:46 PM, arun kirshna [via R] < ml-node+s789695n4676510h87@n4.nabble.com> wrote:> Hi Elio, > Try this: > library(stringr) > lines1<-str_trim(gsub("\t"," ",readLines("elio.txt"))) > lst1<-lapply(split(lines1,cumsum(lines2=="")),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)) > > lst2[1:2] > #$m1 > # aa5 aa10 b253 b254 > #aa5 0 1 1 1 > #aa10 1 0 1 1 > #b253 1 1 0 1 > #b254 1 1 1 0 > # > #$m2 > # aa5 aa9 b27 b29 > #aa5 0 1 1 1 > #aa9 1 0 1 1 > #b27 1 1 0 1 > #b29 1 1 1 0 > > > A.K. > > ________________________________ > From: Elio Shijaku <[hidden email]<http://user/SendEmail.jtp?type=node&node=4676510&i=0>> > > To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4676510&i=1>> > > Sent: Thursday, September 19, 2013 3:27 AM > Subject: Re: binary symmetric matrix combination > > > > Hi Arun, > > Please find attached the text file. Let me know if anything else is > needed. Thanks a lot for your continuous help. > > Best, > > Elio > > ______________________________________________ > [hidden email] <http://user/SendEmail.jtp?type=node&node=4676510&i=2>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. > > > ------------------------------ > 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-tp4675440p4676510.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-tp4675440p4676522.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
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