Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to operate the following function on a vector (of t) but I get the following error:

    rho.prime <- function(t, k=19000){
  for (i in seq(1,length(t))){
    if (abs(t[i]) <= k)
    { print(2*t[i])
      return(2*t[i])
    }
    else 
    {
      print(2*k*sign(t[i]))
      return(2*k*sign(t[i]))
    }
  }
}

Here's the rho.prime function description: enter image description here

The problem is that I am suspicious about the result:

> t=c(1,3,5,7,10)
> t
[1]  1  3  5  7 10
> rho.prime(t,k)
[1] 2
[1] 2
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
145 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your function could look like this:

rho<-function(t,k) ifelse(abs(t)<=k,t^2,(2*k*abs(t))-k^2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...