Hi r-help-boun...@r-project.org napsal dne 03.08.2010 13:17:16:
> Ok thanks, but I have found the solution: "ifelse". > > Here goes a working version of the code: > > mod = function(x) { > ifelse(x < 0,x*(-1),x) > } But before you reinvent wheel try to look if there is not available function already > system.time(y1<-mod(x)) user system elapsed 4.09 0.56 4.68 > system.time(y2<-abs(x)) user system elapsed 0.10 0.02 0.11 > all.equal(y1,y2) [1] TRUE > Regards Petr > 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) > > best regards > > > On Tue, Aug 3, 2010 at 8:03 AM, Pablo Cerdeira <pablo.cerde...@gmail.com> 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 > > > > 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 > > } > > } > > 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) > > > > best regards, > > > > -- > > Pablo de Camargo Cerdeira > > pa...@fgv.br > > pablo.cerde...@gmail.com > > +55 (21) 3799-6065 > > > > > > -- > Pablo de Camargo Cerdeira > pa...@fgv.br > pablo.cerde...@gmail.com > +55 (21) 3799-6065 > > ______________________________________________ > 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. ______________________________________________ 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.