一直以爲有關使更多的東西靠近你在Python做了什麼:
add <- function(x,y){
number_types <- c('integer', 'numeric', 'complex')
if(class(x) %in% number_types && class(y) %in% number_types){
z <- x+y
z
} else stop('Either "x" or "y" is not a numeric value.')
}
在行動:
> add(3,7)
[1] 10
> add(5,10+5i)
[1] 15+5i
> add(3L,4)
[1] 7
> add('a',10)
Error in add("a", 10) : Either "x" or "y" is not a numeric value.
> add(10,'a')
Error in add(10, "a") : Either "x" or "y" is not a numeric value.
注意在R裏面我們只有integer
,numeric
和complex
爲基本數字數據類型。
最後,我不知道錯誤處理是否是你想要的,但希望它有幫助。
你應該嘗試從語法 – MIRMIX