讓我們假設下一個函式:
demo_function < - function(x){
if(is. na(x)){
return(NA)
} else if(1 < x < 2){
return("something")
} else {
return("Nothing")
}
}
這個想法是,當引數x在1和2之間時,例如x=0.001,那么這個函式就會回傳一些東西。
然而,當試圖運行上述函式時,出現了下一個錯誤:
。
錯誤:沒有函式,跳轉到更高的層次
錯誤:沒有函式,跳轉到更高的層次。
我怎樣才能調整這個函式,以便為指定的引數獲得"something"?
uj5u.com熱心網友回復:
問題出在else if中,即R中的語法與數學符號不同--多個運算式是由邏輯運算子連接的
else if(1 < x && x < 2)
即:
demo_function < - function(x){
if(is. na(x)){
return(NA)
} else if(1 < x && x < 2){
return("something")
} else {
return("Nothing")
}
}
> demo_function(0.01)
[1] "Nothing"/span>
> demo_function(1.5)
[1] "something"/span>
> demo_function(NA)
[1] NA
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/309269.html
標籤:
上一篇:如何實作職能系統的自動化
