编写一个对R中的向量进行操作的函数 [英] Writing a function that operates on a vector in R
本文介绍了编写一个对R中的向量进行操作的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
rho 我试图在vector上运行下面的函数。 (1,length(t))){
if(abs(t [i])≤= kri(t) )
{print(2 * t [i])
return(2 * t [i])
}
else
{
print(2 * 2 * k * sign(t [i]))
}
}
}
返回code>
以下是rho.prime函数的描述:
问题是,我怀疑结果:
> t = c(1,3,5,7,10)
> t
[1] 1 3 5 7 10
> rho.prime(t,k)
[1] 2
[1] 2
rho <-function(t) ,k)ifelse(abs(t)≤k,t ^ 2,(2 * k * abs(t)) - k ^ 2)
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:
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
解决方案
Your function could look like this:
rho<-function(t,k) ifelse(abs(t)<=k,t^2,(2*k*abs(t))-k^2)
这篇关于编写一个对R中的向量进行操作的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文