-1

我想知道如何制作一些东西来检查数据中的 Lair 列

低于或高于某个阈值可以说低于 0.5 称为 LOH en 高于称为不平衡。所以调用 LOH 和 INBALANCE 应该写在一个新的列中。我尝试了以下代码。

detection<-function(assay,method,thres){
  if(method=="threshold"){
    idx<-ifelse(segmenten["intensity"]<1.1000000 & segmenten["intensity"]>0.900000 & segmenten["Lair"]>thres,TRUE,FALSE)
  }
  if(method=="cnloh"){
    idx<-ifelse(segmenten["intensity"]<1.1000000 & segmenten["intensity"]>0.900000 & segmenten["Lair"]<thres,TRUE,FALSE)
  }
  if(method=="gain"){
    idx<-ifelse(segmenten["intensity"]>1.1000000 & segmenten["Lair"]<thres,TRUE,FALSE)
  }
  if(method=="loss"){
    idx<-ifelse(segmenten["intensity"]<0.900000 & segmenten["Lair"]<thres,TRUE,FALSE)
  }
  if(method=="bloss"){
    idx<-ifelse(segmenten["intensity"]<0.900000 & segmenten["Lair"]>thres,TRUE,FALSE)
  }
  if(method=="bgain"){
    idx<-ifelse(segmenten["intensity"]>1.100000 & segmenten["Lair"]>thres,TRUE,FALSE)
  }
  return(idx)
}

在这部分之后,下一步是将函数中的数据写入现有表。任何人都有一个想法

4

1 回答 1

1

由于您想要的结果不够清楚,我做了一些假设并写了一些可能有用或无用的东西。

首先,在您的函数内部有一个未定义的对象 segmenten,我想这是作为输入提供的数据集,然后您使用 ifelse 并且返回结果是TRUEorFALSE但您想要或者LOH满足INBALANCE某些条件时。

您想要INBALANCE何时... & segmenten["Lair"]>thresLOH其他方式(这里...是指条件的另一部分)这将给出一个向量,但是您希望它在主数据集中作为附加列,不是吗?因此,也许这可能是您改进代码的新起点。

detection <- function(assay, method=c('threshold', 'cnloh', 'gain', 'loss', 'bloss', 'bgain'),
                    thres=0.5){
  x <- assay
  idx <- switch(match.arg(method),
         threshold = ifelse(x["intensity"]<1.1 & x["intensity"]>0.9 & x["Lair"]>thres, 'INBALANCE', 'LOH'),
         cnloh     = ifelse(x["intensity"]<1.1 & x["intensity"]>0.9 & x["Lair"]<thres, 'LOH', 'INBALANCE'),
         gain      = ifelse(x["intensity"]>1.1 & x["Lair"]<thres, 'LOH', 'INBALANCE'),
         loss      = ifelse(x["intensity"]<0.9 & x["Lair"]<thres,'LOH', 'INBALANCE'),
         bloss     = ifelse(x["intensity"]<0.9 & x["Lair"]>thres, 'INBALANCE', 'LOH'),
         bgain     = ifelse(x["intensity"]>1.1 & x["Lair"]>thres, 'INBALANCE', 'LOH'))

  colnames(idx) <- 'Checking'
  return(cbind(x, as.data.frame(idx)))
  }

例子:

Data <- read.csv("japansegment data.csv", header=T)

result <- detection(Data, method='threshold', thres=0.5) # 'threshold' is the default value for method
head(result)
       SNP_NAME x0 x1 y pos.start   pos.end chrom count copynumber intensity allele.B   Lair uncertain sample_id
1 SNP_A-1656705  0  0 0    836727  27933161     1   230          2    1.0783        1 0.9218     FALSE GSM288035
2 SNP_A-1677548  0  0 0  28244579 246860994     1  4408          2    0.9827        1 0.9236     FALSE GSM288035
3 SNP_A-1669537  0  0 0    100819 159783145     2  3480          2    0.9806        1 0.9193     FALSE GSM288035
4 SNP_A-1758569  0  0 0 159783255 159791136     2     5          2    1.7244        1 0.9665     FALSE GSM288035
5 SNP_A-1662168  0  0 0 159817465 168664268     2   250          2    0.9786        1 0.9197     FALSE GSM288035
6 SNP_A-1723506  0  0 0 168721411 168721920     2     2          2    1.8027       -4     NA     FALSE GSM288035
   Checking
1 INBALANCE
2 INBALANCE
3 INBALANCE
4       LOH
5 INBALANCE
6       LOH

使用match.argswitch函数将帮助您避免大量if语句。

于 2012-09-06T10:15:27.590 回答