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.
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!
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