[R] binary symmetric matrix combination
arun
smartpink111 at yahoo.com
Wed Sep 18 16:31:41 CEST 2013
Hi Elio,
Try this:
Assuming that there is a single blank row separating the matrices:
lines1<- readLines(textConnection("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"))
lst1<-lapply(split(lines1,cumsum(lines1=="")),function(x) as.matrix(read.table(text=x[x!=""],sep="",row.names=1)))
names(lst1)<- paste0("m",seq_along(lst1))
lst1[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
#Reading from file: The data you showed seems to be tab separated. It could be different.
lines2<- gsub("\t","", readLines("Elio.txt"))
##In case it is just space:
#lines2<- readLines("Elio.txt")
lst2<-lapply(split(lines2,cumsum(lines2=="")),function(x) as.matrix(read.table(text=x[x!=""],sep="",row.names=1)))
names(lst2)<- paste0("m",seq_along(lst2))
identical(lst1,lst2)
#[1] TRUE
A.K.
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!!
More information about the R-help
mailing list