Splitting a pairwise matrix in R

#1
I have a pairwise matrix as follows
#### sample matrix
DF <- read.table(text="gene_ids A-B A-C A-D A-E B-C B-D B-E C-D C-E **D-E**
GENE1 0 0 1 1 1 0 1 0 1 1
GENE2 1 0 1 1 1 1 0 1 1 0
GENE3 1 0 0 0 1 1 0 1 0 1
GENE4 0 1 0 0 0 0 1 1 1 0
GENE5 1 1 0 0 0 0 0 1 1 1
GENE6 0 1 1 0 0 1 0 0 0 1", header=TRUE)


and used the code

m_temp <- matrix(NA,ncol=5,nrow=5)
up <- upper.tri(m_temp)
lo <- lower.tri(m_temp)
lapply(seq_len(nrow(DF)), function(i, res) {
tmpnames <- do.call(rbind,strsplit(names(unlist(DF[i, -1])),"\\."))
rownames(res) <- c(tmpnames[1,1],tmpnames[tmpnames[,1]==tmpnames[1,1],2])
res[up] <- unlist(DF[i, -1])
res[lo] <- unlist(DF[i, -1])
res <- cbind(res, rowSums(res, na.rm = TRUE))
colnames(res) <- c(rownames(res),"sum")
res
} , res = m_temp)


In order to convert it into the following form ( splitting a pairwise matrix into individual vectors for each gene):

[[gene 1]]
A B C D E sum
A NA 0 0 1 1 2
B 0 NA 1 1 0 2
C 0 1 NA 0 1 2
D 1 0 0 NA 1 2
E 1 1 1 1 NA 4

[[gene 2]]
A B C D E sum
A NA 1 0 1 0 2
B 1 NA 1 1 1 4
C 0 1 NA 1 1 3
D 1 1 1 NA 0 3
E 1 0 1 0 NA 2

[[gene 3]]
A B C D E sum
A NA 1 0 0 0 1
B 1 NA 0 1 1 3
C 0 1 NA 1 0 2
D 0 1 1 NA 1 3
E 0 0 0 1 NA 1

[[gene 4]]
A B C D E sum
A NA 0 1 0 1 2
B 0 NA 0 0 1 1
C 1 0 NA 0 1 2
D 0 0 1 NA 0 1
E 0 1 1 0 NA 2

[[gene 5]]
A B C D E sum
A NA 1 1 0 0 2
B 1 NA 0 0 1 2
C 1 0 NA 0 1 2
D 0 0 1 NA 1 2
E 0 0 1 1 NA 2

[[gene 6]]
A B C D E sum
A NA 0 1 0 0 1
B 0 NA 1 0 0 1
C 1 0 NA 1 0 2
D 1 1 0 NA 0 2
E 0 0 0 0 NA 0
The code works perfectly in this case but when there is a missing combination as shown below then it starts generating error and has random values in the matrix after converting.

sample matrix ( In this case the combination D-E is missing)
DF <- read.table(text="gene_ids A-B A-C A-D A-E B-C B-D B-E C-D C-E
GENE1 0 0 1 1 1 0 1 0 1
GENE2 1 0 1 1 1 1 0 1 1
GENE3 1 0 0 0 1 1 0 1 0
GENE4 0 1 0 0 0 0 1 1 1
GENE5 1 1 0 0 0 0 0 1 1
GENE6 0 1 1 0 0 1 0 0 0 ", header=TRUE)
and I get instead some matrices which have some altered values:

[[1]]
A B C D E sum
A NA 0 0 1 1 2
B 0 NA 1 1 0 2
C 0 1 NA 0 1 2
D 1 0 0 NA 0 1
E 1 1 1 0 NA 3

[[2]]
A B C D E sum
A NA 1 0 1 0 2
B 1 NA 1 1 1 4
C 0 1 NA 1 1 3
D 1 1 1 NA 1 4
E 1 0 1 1 NA 3

[[3]]
A B C D E sum
A NA 1 0 0 0 1
B 1 NA 0 1 1 3
C 0 1 NA 1 0 2
D 0 1 1 NA 1 3
E 0 0 0 1 NA 1

[[4]]
A B C D E sum
A NA 0 1 0 1 2
B 0 NA 0 0 1 1
C 1 0 NA 0 1 2
D 0 0 1 NA 0 1
E 0 1 1 0 NA 2

[[5]]
A B C D E sum
A NA 1 1 0 0 2
B 1 NA 0 0 1 2
C 1 0 NA 0 1 2
D 0 0 1 NA 1 2
E 0 0 1 1 NA 2

[[6]]
A B C D E sum
A NA 0 1 0 0 1
B 0 NA 1 0 0 1
C 1 0 NA 1 0 2
D 1 1 0 NA 0 2
E 0 0 0 0 NA 0
There were 12 warnings (use warnings() to see them)

Since there is a missing combination then automatically the values inside the matrix changes. Iam unable to change this in the code to check for missing combinations .
 
Last edited:
#2
Some text would be fine. Now it seems like “it is all in the code”. And at least some explanation about what is intended to be done. Or at least a question. People are not so good with mind reading.

There is an “edit button”!



Edit: I still have no idea what the poster is aiming at, what the code is intended to do and I can't see any question. Most of the time posts like this are just ignored.