简体   繁体   English

里面的 if-else 语句 apply function R

[英]If-else statement inside apply function R

I am writing an apply function in R to search a table and return all the instances that TRUE occurs, and I have written the following code but it keeps giving me errors, and I am not sure why.我正在 R 中编写应用 function 来搜索表并返回所有出现 TRUE 的实例,并且我编写了以下代码,但它一直给我错误,我不知道为什么。 Any help is appreciated.任何帮助表示赞赏。



xsum = apply(genomeTable, 1, function(i) {

    if (i) < q.start | if (i) > q.end{
      return FALSE

    } else{
      return TRUE
    }
  })

sum(xsum)

Your if statement is duplicated.您的if语句重复。 You only need one if for the condition.你只需要一个if条件。 Parenthesis need to wrap the whole condition.括号需要包裹整个条件。 Try this:尝试这个:

xsum = apply(genomeTable, 1, function(i) {
  if (i < q.start | i > q.end) {
    return(FALSE)
  } else {
    return(TRUE)
  }
})

Try this:尝试这个:

xsum = apply(genomeTable, 1, function(i) ifelse ((i < q.sta | i > q.end), FALSE, TRUE)) 

It does not work, please provide some data.没用,请提供一些数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM