一直以为有关使更多的东西靠近你在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