在R中使用if-else语句发出警告
我的数据与此相似在R中使用if-else语句发出警告,r,if-statement,R,If Statement,我的数据与此相似 start end strand 45 52 + 66 99 - 我们把这个叫做表1 如果绞线中有一个+,我必须返回两个值,从起始值算起+/-10 所以,这里我必须返回55和35 如果我有一个-in串,我必须返回两个值,这是从结束值+/-10 为此,我编写了以下程序: if(table1$strand == '+'){ newstart = table1$start - 10 newend = table1$start + 10 } else { ne
start end strand
45 52 +
66 99 -
我们把这个叫做表1
如果绞线中有一个+,我必须返回两个值,从起始值算起+/-10
所以,这里我必须返回55和35
如果我有一个-in串,我必须返回两个值,这是从结束值+/-10
为此,我编写了以下程序:
if(table1$strand == '+'){
newstart = table1$start - 10
newend = table1$start + 10
} else {
newstart = table1$end - 10
newend = table1$end + 10
}
但是,我得到了这个警告信息:
条件的长度大于1,并且只使用第一个元素
有没有办法使用矢量化方法来避免这种情况?您想使用
ifelse
来矢量化流程:
ifelse(table1$strand == '+', table1$start, table1$end)
这可以一步完成所有工作:
> outer(ifelse(table1$strand == '+', table1$start, table1$end), c(10, -10), `+`)
[,1] [,2]
[1,] 55 35
[2,] 109 89
要使用
ifelse
对流程进行矢量化:
ifelse(table1$strand == '+', table1$start, table1$end)
这可以一步完成所有工作:
> outer(ifelse(table1$strand == '+', table1$start, table1$end), c(10, -10), `+`)
[,1] [,2]
[1,] 55 35
[2,] 109 89
下面是一个使用
ifelse
的示例。如果这是您的示例数据
table1<-structure(list(start = c(45L, 66L), end = c(52L, 99L), strand = structure(c(2L,
1L), .Label = c("-", "+"), class = "factor")), .Names = c("start",
"end", "strand"), class = "data.frame", row.names = c(NA, -2L))
table1下面是一个使用ifelse
的示例。如果这是您的示例数据
table1<-structure(list(start = c(45L, 66L), end = c(52L, 99L), strand = structure(c(2L,
1L), .Label = c("-", "+"), class = "factor")), .Names = c("start",
"end", "strand"), class = "data.frame", row.names = c(NA, -2L))
table1table1$strand=='+'
重新运行逻辑向量,仅其第一个值用于计算if语句。请解释您对上述示例的预期结果?是的,有没有办法消除此警告message@user3683555问“有没有办法消除警告信息?”就像问“有没有办法关闭检查发动机指示灯?”table1$strand=='+'
重新运行逻辑向量,仅其第一个值用于计算if语句。请解释您对上述示例的预期结果?是的,有没有办法消除此警告message@user3683555问“有没有办法消除警告信息?”就像问“有没有办法关闭检查发动机指示灯?”