Creating a matrix of sums from samples

I have one mutation count matrix, like "counts". This matrix has the column names V1, V2, ..., Vi, ... Vn, where not all "i" are. Thus, he can jump, for example, V1, V2, V5. In addition, most columns have 0.

I need to create a sum matrix called an β€œanswer”, where the element i, j is the sum of the number of number counters for both i and j. In element i, I just show the number of samples in i.

Here you can quickly configure data. I already have the correct dimensional matrix installed in my code called "answer". Thus, I need to automate the last few rows where I fill the matrix.

counts <- matrix(data = c(0,2,0,5,0,6,0), nrow = 1, ncol = 7, dimnames=list("",c("V1","V2","V3","V4","V5","V6","V7"))) answer <- matrix(data =0, nrow = 3, ncol = 3, dimnames = list(c("V2","V4","V6"),c("V2","V4","V6"))) answer[1,1] <- 2 answer[1,2] <- 7 answer[1,3] <- 8 answer[2,1] <- 7 answer[2,2] <- 5 answer[2,3] <- 11 answer[3,1] <- 8 answer[3,2] <- 11 answer[3,3] <- 6 

I understand that I can do this with two nested loops, but of course there should be a better way, no? Thanks!

+4
source share
1 answer

This can be done with the correct use of expand.grid and rowSums :

 n = counts[, counts > 0] answer = matrix(rowSums(expand.grid(n, n)), nrow=length(n), dimnames=list(names(n), names(n))) diag(answer) = n 

To show how this works, n will look like this:

 V2 V4 V5 2 5 6 

and expand.grid(n, n) :

  Var1 Var2 1 2 2 2 5 2 3 6 2 4 2 5 5 5 5 6 6 5 7 2 6 8 5 6 9 6 6 

The last line ( diag ) is necessary, because otherwise the diagonal will be twice as large as the original vector (adding 2+2 , 5+5 or 6+6 ).

+4
source

All Articles