最近很多人借TIDE账号,也有人问我如何进行数据标准化,按照网站说明,对于没有Control样本的测序数据,把表达量减去每个基因在所有样本中的平均值即可,即按照行计算平均值,再拿这一行所有表达量-该平均值。

#读取数据
Expr <- read.delim(\"TIDE_COAD.txt\",sep = \"\\t\",row.names = 1)
#使用apply函数
Expr <- t(apply(Expr, 1, function(x)x-(mean(x))))
一句代码就得到标准化矩阵
测试代码:
ma <- matrix(c(1:4, 1, 6:8), nrow = 2)
ma
# [,1] [,2] [,3] [,4]
#[1,] 1 3 1 7
#[2,] 2 4 6 8
apply(ma, 2, function(x)x-(mean(x))) ##按列计算
# [,1] [,2] [,3] [,4]
#[1,] -0.5 -0.5 -2.5 -0.5
#[2,] 0.5 0.5 2.5 0.5
apply(ma, 1, function(x)x-(mean(x))) ##按行计算
# [,1] [,2]
#[1,] -2 -3
#[2,] 0 -1
#[3,] -2 1
#[4,] 4 3
转置一下即可:
t(apply(ma, 1, function(x)x-(mean(x))))
# [,1] [,2] [,3] [,4]
#[1,] -2 0 -2 4
#[2,] -3 -1 1 3
apply函数参数帮助。
apply(X, MARGIN, FUN, …)
Arguments
X
an array, including a matrix.
MARGIN
a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names.
FUN
the function to be applied: see ‘Details’. In the case of functions like +, %*%, etc., the function name must be backquoted or quoted.
…
optional arguments to FUN.
暂无评论内容