检查列旁边的列是否有正确的true或false值

检查列旁边的列是否有正确的true或false值,r,parameters,R,Parameters,我想知道如何做一些东西来检查列是否在 低于或高于某个阈值(比如低于0.5)称为LOH en 以上称为不平衡。因此,LOH和INBALANCE的调用应该写在一个新的列中。我尝试了下面的代码 detection<-function(assay,method,thres){ if(method=="threshold"){ idx<-ifelse(segmenten["intensity"]<1.1000000 & segmenten["intensity"]&g

我想知道如何做一些东西来检查列是否在

低于或高于某个阈值(比如低于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)
}

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

首先,在您的函数中有一个未定义的对象分段en,我假设这是作为输入提供的数据集,然后使用ifelse,返回的结果是
TRUE
FALSE
,但在满足某些条件时,您需要
LOH
不平衡

时,您需要
不平衡
segmenten[“Lair”]>thres
LOH
否则(这里
..
表示条件的另一部分)这将给出一个向量,但您希望它作为附加列出现在主数据集中,不是吗?因此,这可能是您改进代码的一个新起点

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)))
  }

检测无需为其定义函数。只需使用
因子(强度>thres,级别=c(假,真),标签=c(“LOH”,“失衡”))
@Backlin谢谢!剩下的一个问题是阈值的表现如何,是否可以给出评分3个参数,如LOH、平衡和不平衡
因子(findInterval(强度,thres),级别=0:2,标签=c(…)
其中
thres
是两个分隔组的分界点。你着手解决这个问题的方式建议你应该阅读R的基础知识。有很多好书可供选择,既然你似乎知道如何编程,我建议你。在转到更高级的主题之前,它简要介绍了基础知识(没有不必要的赘述)。“我觉得它很有用。”巴克林很抱歉我读了你的最后一条评论。这本书是一个很好的选择,谢谢你的提示!
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