I am trying to normalize the values in a matrix with respect to my controls (R247, R235, R241).
My coldata
is:
Condition Tank
R235 Control T6
R236 LowExposure T6
R239 HighExposure T6
R241 Control T8
R242 LowExposure T8
R245 HighExposure T8
R247 Control T14_3
R248 LowExposure T14_3
R250 HighExposure T14_3
and my matrix mydata
:
R235 R236 R239 R241 R242 R245 R247 R248 R250
ENSDARG00000033160 11.91873 10.899929 10.831388 12.092478 11.564555 10.908011 11.67680 11.168115 10.414632
ENSDARG00000013522 12.39036 11.692673 11.439107 12.440952 11.841307 11.118888 12.13594 11.634806 11.336330
ENSDARG00000103295 10.54697 10.004169 8.753556 10.659075 9.980232 8.511240 11.11711 10.690518 9.240825
ENSDARG00000056765 9.18106 8.488917 7.431641 9.440119 8.830816 7.901337 10.39879 9.899546 8.142807
ENSDARG00000087303 11.07447 10.765197 11.682291 11.010172 10.380666 11.487207 11.05384 10.526109 11.962465
ENSDARG00000018478 11.51562 11.000702 10.382845 11.597848 11.218944 10.185381 11.61043 11.214280 10.614338
I extract the control samples via:
x <- which(coldata$Condition %in% "Control")
control <- row.names(coldata[x,])
Similar to a z-score transformation I would like to use the mean and sd but only from the control groups to transform the dataset like (x - mean[control]) / sd[control] with something like:
function(x){
(x - rowMeans[,control])/apply(matrix[,control],1,sd)
}
and then use apply()
to run this over mydata
like: apply(mydata, 1, function(x))
but I don't know how to properly write this as a function that can be used via apply. Any help is highly appreciated. Thx!