for循环与R中的if语句一起?

时间:2017-09-21 21:09:16

标签: r for-loop

有人能用这个小功能来帮助我吗?

opt <- function( x, shape, scale){
  k=shape
  wpd1=0.3395
  wpd2=c()
  for(i in 1:101){
  wpd = mean((k/scale) * x^k)
  if(wpd==wpd1){wpd2=wpd}else{k=k+(i-1)/100 -0.5}
  }
  return(wpd2)
}

当我使用opt(rnorm(10),2,3)时,我收到错误:

Error in if (wpd == wpd1) { : missing value where TRUE/FALSE needed

1 个答案:

答案 0 :(得分:0)

mean((k/scale) * x^k)更改为mean((k/scale) * x^k, na.rm = TRUE)您在运行(-#)^(2/3)

时想要获得NaN