[R] binary symmetric matrix combination
arun
smartpink111 at yahoo.com
Fri Sep 6 00:54:19 CEST 2013
HI,
No problem.
I think you didn't run the `vecOut` after adding the new matrix. `lst1` is based on `vecOut`
For example:
m5<- as.matrix(read.table(text="y1 e6 l16
y1 0 1 1
e6 1 0 1
l16 1 1 0",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))
lst1<-sapply(paste0("m",1:5),function(x) {x1<- get(x); x2<-paste0(colnames(x1)[col(x1)],rownames(x1)[row(x1)]); match(x2,vecOut)})
lst1
#$m1
#[1] 1 2 11 12
#
#$m2
#[1] 1 3 4 5 21 23 24 25 31 33 34 35 41 43 44 45
#
#$m3
#[1] 1 6 7 8 51 56 57 58 61 66 67 68 71 76 77 78
#
#$m4
#[1] 1 9 10 81 89 90 91 99 100
#
#$m5
#[1] 1 NA NA NA NA NA NA NA NA ########
###Here vecOut was based on Out2
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)
#Error in Out[lst1[[i]]] <- lst2[[i]] :
# NAs are not allowed in subscripted assignments
###Running vecOut using Out3
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)})
fn1(N,Out3)
# y1 g24 c1 c2 l17 h4 s2 s30 e5 l15 e6 l16
#y1 0 1 1 1 1 1 1 1 1 1 1 1
#g24 1 0 0 0 0 0 0 0 0 0 0 0
#c1 1 0 0 1 1 0 0 0 0 0 0 0
#c2 1 0 1 0 1 0 0 0 0 0 0 0
#l17 1 0 1 1 0 0 0 0 0 0 0 0
#h4 1 0 0 0 0 0 1 1 0 0 0 0
#s2 1 0 0 0 0 1 0 1 0 0 0 0
#s30 1 0 0 0 0 1 1 0 0 0 0 0
#e5 1 0 0 0 0 0 0 0 0 1 0 0
#l15 1 0 0 0 0 0 0 0 1 0 0 0
#e6 1 0 0 0 0 0 0 0 0 0 0 1
#l16 1 0 0 0 0 0 0 0 0 0 1 0
A.K.
Thanks a lot, all the codes worked perfectly. I have an additional question on the last steps you mentioned.
I wanted to add another matrix to the ones I gave as an example,
inputing m5 worked well, however when I type the code (added colnames
(m5), changed 1:4 with 1:5 and added m5 to list2 I get the following
error:
Error in Out[lst1[[i]]] <- lst2[[i]] :
NAs are not allowed in subscripted assignments
Below is the code: (am I doing something wrong? very many thanks again for helping!!
names1<-unique(c(colnames(m1),colnames(m2),colnames(m3),colnames(m4), colnames(m5)))
Out3<-matrix(0,length(names1),length(names1),dimnames=list(names1,names1))
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)
----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc:
Sent: Thursday, September 5, 2013 4:30 PM
Subject: Re: binary symmetric matrix combination
Also, some of the steps could be reduced by:
names1<-unique(c(colnames(m1),colnames(m2),colnames(m3),colnames(m4)))
Out3<-matrix(0,length(names1),length(names1),dimnames=list(names1,names1))
lst1<-sapply(paste0("m",1:4),function(x) {x1<- get(x); x2<-paste0(colnames(x1)[col(x1)],rownames(x1)[row(x1)]); match(x2,vecOut)})
lst2<- list(m1,m2,m3,m4)
N<- length(lst1)
fn1<- function(N,Out){
i=1
while(i<=N){
Out[lst1[[i]]]<-lst2[[i]]
i<-i+1
}
Out
}
fn1(N,Out3)
# 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
identical(Out2,fn1(N,Out3))
#[1] TRUE
A.K.
----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc:
Sent: Thursday, September 5, 2013 4:09 PM
Subject: Re: binary symmetric matrix combination
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!
More information about the R-help
mailing list