[R] binary symmetric matrix combination

arun smartpink111 at yahoo.com
Sat Sep 21 17:32:43 CEST 2013


Hi Elio,
Use ?write.table()
write.table(res,"Eliores.txt",quote=FALSE)

#and as read the file
mat1<- as.matrix(read.table("Eliores.txt",header=TRUE))
#or even this should work
mat1<-  as.matrix(read.table("Eliores.txt"))

#If you have very big matrix, you may try:
library(tseries)
write(res,"ElioresNew.txt",ncolumns=dim(res)[1]) #dimnames are not stored
write.table(dimnames(res)[[1]],"Eliodimnames.txt",quote=FALSE,row.names=FALSE)
mat2<- read.matrix("ElioresNew.txt")
 names1<-read.table("Eliodimnames.txt",header=TRUE,stringsAsFactors=FALSE)[,1]
dimnames(mat2)<- list(names1,names1)
all.equal(mat1,mat2)
#[1] TRUE

A.K.




________________________________
From: Elio Shijaku <selius at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Sent: Saturday, September 21, 2013 5:10 AM
Subject: Re: binary symmetric matrix combination



Hi Arun, 

That
 was real magic, thanks a lot, now I get the full matrix with no need to
 input the commands that you gave me before. One last question, how to 
export the res matrix as a text file so I can use it with other 
software?






On Sat, Sep 21, 2013 at 4:35 AM, arun <smartpink111 at yahoo.com> wrote:

HI Elio,
>It looks like you haven't installed reshape2 package. Or is there any problem in installing the package?
>install.packages("reshape2")
>
>
>
>
>Hi Arun,
>
>I get the following error when I enter the command:
>
>library(reshape2)
>
>
>Error in library(reshape2) : there is no package called ‘reshape2’
>
>
>Any ideas?
>
>
>Thanks again.
>



----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Friday, September 20, 2013 8:59 AM
Subject: Re: binary symmetric matrix combination

>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.



More information about the R-help mailing list