Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a matrix of 0s and 1s based on vector labels

Tags:

r

matrix

I have a vector:

A <- c("CTRL", "A", "B", "C")

I would like to generate this matrix:

             [A]   [B]  [C] [CTRIL
[A-CTRL]      1     0.   0.   -1
[B-CTRL]      0.    1.   0.   -1
[C-CTRL]      0.    0.   1.   -1

I did write something like this:

levels(global.proteinSummarization$ProteinLevelData$GROUP)

comparison1 <- matrix(c(1,-1,0,0,0,0,0,0), nrow=1)
comparison2 <- matrix(c(0,-1,1,0,0,0,0,0), nrow=1)
comparison3 <- matrix(c(0,-1,0,1,0,0,0,0), nrow=1)

comparison <- rbind(comparison1, comparison2, comparison3)
row.names(comparison) <- c("A-CTRL", "B-CTRL", "C-CTRL")
colnames(comparison) <- levels(global.proteinSummarization$ProteinLevelData$GROUP) 

However, is there any more automatic way?

like image 331
Paolo Lorenzini Avatar asked Oct 19 '25 15:10

Paolo Lorenzini


1 Answers

You can produce a "sum to zero" contrast matrix.

t(contr.sum(c("A", "B", "C", "CTRL")))

#      A B C CTRL
# [1,] 1 0 0   -1
# [2,] 0 1 0   -1
# [3,] 0 0 1   -1
like image 63
Darren Tsai Avatar answered Oct 22 '25 05:10

Darren Tsai