[英]warning with if else statement in R
我的數據看起來與此相似
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
的示例。 如果這是您的樣本數據
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))
那你可以做
newstart <- ifelse(table1$strand=="+", table1$start, table1$end)-10
newend <- newstart + 20
一次對所有行進行操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.