Pablo Cerdeira
2010-Aug-03 11:03 UTC
[R] The condition has length > 1 and only the first element will be used
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 pablo at fgv.br pablo.cerdeira at gmail.com +55 (21) 3799-6065
Petr PIKAL
2010-Aug-03 11:15 UTC
[R] Odp: The condition has length > 1 and only the first element will be used
Hi r-help-bounces at r-project.org napsal dne 03.08.2010 13:03:33:> Hi All, > > I'm trying to run the following script in R, but I'm getting a warningsaying:> > 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?Your function takes only one scalar argument so you can not use it on vector. Hint. Look at See also help page of if command. Regards Petr> > 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 > pablo at fgv.br > pablo.cerdeira at gmail.com > +55 (21) 3799-6065 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
jim holtman
2010-Aug-03 11:16 UTC
[R] The condition has length > 1 and only the first element will be used
Look at the help page for 'if': "A length-one logical vector that is not NA. Conditions of length greater than one are accepted with a warning, but only the first element is used. Other types are coerced to logical if possible, ignoring any class." If you want the positive value, why aren't you using 'abs'? You might want to look at the 'ifelse' function to work with vectors. You would do something like: mod <- ifelse(x < 0, x * (-1), x) You probably need to re-read the Intro to R to understand more about vectorized operators/operations. On Tue, Aug 3, 2010 at 7:03 AM, Pablo Cerdeira <pablo.cerdeira at 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 > pablo at fgv.br > pablo.cerdeira at gmail.com > +55 (21) 3799-6065 > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Pablo Cerdeira
2010-Aug-03 11:17 UTC
[R] The condition has length > 1 and only the first element will be used
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) } 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.cerdeira at 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 > pablo at fgv.br > pablo.cerdeira at gmail.com > +55 (21) 3799-6065 >-- Pablo de Camargo Cerdeira pablo at fgv.br pablo.cerdeira at gmail.com +55 (21) 3799-6065
Gerrit Eichner
2010-Aug-03 11:17 UTC
[R] The condition has length > 1 and only the first element willbeused
Hello, Pablo, if() doesn't accept a vector as its argument the way you may want it to; take a look at ifelse(). Regards -- Gerrit On Tue, 3 Aug 2010, 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 > > 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 > pablo at fgv.br > pablo.cerdeira at gmail.com > +55 (21) 3799-6065 > > ______________________________________________ > R-help at 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. >--------------------------------------------------------------------- AOR Dr. Gerrit Eichner Mathematical Institute, Room 212 gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/~gcb7
David Winsemius
2010-Aug-03 11:17 UTC
[R] The condition has length > 1 and only the first element will be used
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 usedifelse 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