【发布时间】:2018-03-07 01:04:08
【问题描述】:
我意识到其他帖子中提到了这个错误,但我仍然无法弄清楚它如何适用于我的特定情况。我有以下代码。
myfun <- function(x, g, o){
y <- x
fs <- ((g-1)/o) * (o*g/((g-1)*(1+o)))^g
xb <- o/(g-1)
y[x>=xb] <- ((x+o)/(1+o))^g
y[x<xb] <- x*fs
return(y)
}
x <- seq(0,1,length=5)
y <- myfun(x, 1.5, 0.05)
我的代码返回以下错误。
Warning messages:
1: In y[y >= xb] <- ((x + o)/(1 + o))^g :
number of items to replace is not a multiple of replacement length
2: In y[y < xb] <- x * fs :
number of items to replace is not a multiple of replacement length
此外,结果似乎不正确。
我期待
y =
0 0.152720709664243 0.379105500429200 0.665044998814453 1
但得到:
y =
[1] 0.00000000 0.01039133 0.15272071 0.37910550 0.66504500
这让我相信我在索引中做错了什么,或者向量 x 上的数学有问题。任何帮助将非常感激。
【问题讨论】:
标签: r