I am trying to create a vectorial function in R using the command ifelse
as follows
temp <- function(p){
ifelse(p < 0.5 *(1 + 0.5), (1 + 0.5) * qnorm(p/(1 +0.5)), (1 - 0.5) * qnorm((p - 0.5)/(1 - 0.5)))
}
The function works well with scalar values, for example
> temp(0.1)
[1] -2.251629
> temp(0.9)
[1] 0.4208106
but not for vectors:
> temp(c(0.1,0.9))
[1] -2.2516289 0.4208106
Warning message:
In qnorm((p - 0.5)/(1 - 0.5)) : NaNs produced
The weird thing is that it returns the right answer, but indicating a warning.
What am I doing wrong? It seems that the ifelse
is evaluating both functions in all the entries of the vector p
, which is supposed to be avoided with this command.