r - 表格中的背景减法

标签 r data.table bioconductor

我有基因表达数据作为每个探针的计数,如下所示:

library(data.table)
mydata <- fread(
"molclass,mol.id,sample1,sample2,sample3
negative, negat1,  0, 1,   2
negative, negat2,  2, 1,   1
negative, negat3,  1, 2,   0
 endogen,  gene1, 30, 15, 10
 endogen,  gene2, 60, 30, 20
")

我的问题是 - 执行背景减法的最佳方法是什么,即对于每个 sampleN我需要计算背景的列(假设它将是 negative 类中所有值的平均值),然后从该列的每个值中减去该背景。目前我正在使用以下解决方案:

for (nm in names(mydata)[-c(1:2)]) {
  bg <- mydata[molclass=='negative', nm, with=F];
  bg <- mean(unlist(bg));
  mydata[[nm]] <- (mydata[[nm]] - bg);
}

但我觉得一定有一些“更好”的方式。

附言我知道有一些包可以做这些事情,但我的数据对应于计数,而不是信号强度 - 所以我不能使用 limma或为微阵列设计的类似工具。也许一些 seq-data 包可以提供帮助,但我不确定,因为我的数据也不是来自测序。

最佳答案

如果您需要用计算值替换 sample 列,您可以使用 set(如@Frank 的帖子中所示)但无需创建额外的对象

indx <- grep('^sample', names(mydata))
for(j in indx){
 set(mydata, i=NULL, j=j, value=mydata[[j]]- 
       mydata[molclass=='negative', mean(unlist(.SD)), .SDcols=j])
}
mydata
#   molclass  mol.id sample1    sample2 sample3
#1: negative  negat1      -1 -0.3333333       1
#2: negative  negat2       1 -0.3333333       0
#3: negative  negat3       0  0.6666667      -1
#4:  endogen   gene1      29 13.6666667       9
#5:  endogen   gene2      59 28.6666667      19

或者@Frank 建议的变体(更有效)

for(j in indx){
 set(mydata, i=NULL, j=j, value=mydata[[j]]- 
    mean(mydata[[j]][mydata$molclass=='negative']))
}

关于r - 表格中的背景减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28597878/

相关文章:

R如何安装指定版本的bioconductor包?

r - Bioconductor 与 CRAN

r - 识别 R 中连续重叠的段

r - 按组填写日期的缺失行

r - 修复 R 中返回栅格单元之间距离数据帧的函数瓶颈

C : explain theory behind

r - 使用 biomaRt 注释位置

r - 在向量中查找第一个 TRUE 值的更快方法

r - 如何找到创建对象的位置?

r - 在 R 中使用 gganimate 创建绘图动画的问题