the condition has length > 1 and only the first element will be used

Hi,
I have created the following function in R:

f<-function(t) {
if (t<0){
f<-0
return(f)
}else{
f<-(2*t)/((1+t^2)^2)
return(f)
}
    }
  

The function works fine but when I try to operate with it or plot it, I get the following error:

In if (t < 0) { : the condition has length > 1 and only the first element will be used

I have tried to used ifelse, as some other post suggested but I can not get any improvement. What can I do to fix it?

Thank you

2 Likes

Could you provide an example code for us to look at (see the suggestion for reprex)? It is really hard to help without some idea of what you are trying to achieve.

1 Like

I agree a reprex would be helpful.

What is happening here is you are trying to pass f() a vector of length > 1. if() and else can only react to one logical element (i.e., a TRUE or a FALSE) at a time. You should either use sapply(t, f), or use ifelse(). ifelse() can respond to a logical vector with length greater than one. First, you should make sure you are intending to pass f() a vector of length > 1. If you are, then write your function using ifelse():

f <- function(t) {
  ifelse(t < 0, 0, (2*t)/((1+t^2)^2)
}

This should work if you run f(t) where t is a numeric vector. The syntax is:

ifelse(condition, do_if_true, do_if_false)

See the section in a book I recently wrote using bookdown on this topic. Hope this helps!

7 Likes

Thank you very much bstaton1, it worked perfectly!!!

:slight_smile:

You can also make your function works on vectors pretty easily with base R Vectorize function.

Currently, your function is made to work on a single element because of the if clause. You could do this so that it applies on vector

f<-function(t) {
  if (t<0){
    f<-0
    return(f)
  }else{
    f<-(2*t)/((1+t^2)^2)
    return(f)
  }
}
# your function is working as a non vectorized function
f(1)
#> [1] 0.5
f(-1)
#> [1] 0
# So error, or here warning on a vector
f(c(1, -1))
#> Warning in if (t < 0) {: la condition a une longueur > 1 et seul le premier
#> élément est utilisé
#> [1]  0.5 -0.5

# make your function vectorized
f_vec <- Vectorize(f, vectorize.args = "t")
# No more warning and wrong result
f_vec(c(1, -1))
#> [1] 0.5 0.0

Created on 2018-10-21 by the reprex package (v0.2.1)

6 Likes

@Irene_Jacob, glad this helped. If it solved your problem, please mark it as solved - this will help others looking for solutions to the same problem

2 Likes

Thank you very much cderv, I also tried as you suggested and it works perfectly fine :slight_smile:

Hi @cderv ,

can you please provide tell me in which line we should below function to get the output in every row?

f_vec <- Vectorize(f, vectorize.args = "t")

I have the similar problem as below example, if you could explain the solution with below example it would be grateful.

Please try to provide the complete
f<-function(t) {
if (t<0){
f<-0
return(f)
}else{
f<-(2*t)/((1+t^2)^2)
return(f)
}
}
f_vec <- Vectorize(f, vectorize.args = "t")