I want to create a multivariate probability density function with R to plot it as a perspective plot. f(x,y) is x+y for [0,1]*[0,1], 0 else. I tried this:
x <- seq(-0.5,1.5,length=50)*seq(0.5,1.5,length=50)
y <- x
f <- function(x,y){
if (0<=x<=1 && 0<=y<=1){
x+y
} else {
0
}
}
f(x,y)
But the function is not working and I don’t know how to fix it. Does anyone have an idea?
First of all your
ifstatement is wrong. This would fix the if statement but would still give the error.You need to vectorize your function in order to give it vector arguments. One way is to use function
ifelsewhich is vectorized version ofifandelse