为什么我的代码在我的r程序中显示错误,使用if else函数?

时间:2015-03-07 17:52:27

标签: r

我尝试编写下面的程序。但是它没有工作。如果x是偶数则x值是正值 if和for x是奇数,它是else。我是编程新手:

gam<-function(x){

if as.numeric(x%%==0){
ans<- factorial((x/2)-1)
}
else
{
ans<-  factorial(x-1)/2^(x-1)*factorial((x/2)-(1/2))
}
return(ans)
}

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:-1)

你的问题是你错过了

 if (x%%2==0)

可能是你想要的东西:

gam<-function(x){

if (x%%2==0){
ans<- factorial((x/2)-1)
}else{
ans<-  factorial(x-1)/2^(x-1)*factorial((x/2)-(1/2))
}
return(ans)
}

只需输入gam(2)或gam(3)等。