On Aug 3, 2010, at 7:03 AM, Pablo Cerdeira wrote:
Hi All,
I'm trying to run the following script in R, but I'm getting a
warning saying:
Warning message:
In if (z < 0) { :
the condition has length > 1 and only the first element will be used
ifelse is the proper function rather than if{}else{}. Read:
?ifelse
?Control
As you can see, I'm sending a vector x to the function f without any
problem. The function f calculates the y value for each x.
But the function f needs to convert the x to positive values (the mod
function). And when it tries to convert, it always uses the first
value of x.
What I'm doing wrong here?
mod = function(x) {
if (x < 0) {
mod <- x*(-1)
}
else {
mod <- x
}
}
Try instead one of:
mod <- function (x) (x < 0)*(-1)*x + (x >= 0)*x
mod <- function (x) ifelse( x < 0 , -x, x)
mod <- abs
f = function(x) {
f <- mod(x)/x
}
x <- seq(-1,1,0.01)
x
y <- f(x)
y
plot(f,xlim = c(-1,1))
remove(x,y,f,mod)
You didn't provide any data, nor did you indicate problems with that
code, so I am not commenting on that.
David Winsemius, MD
West Hartford, CT
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.