![](/img/trans.png)
[英]Error in if (!all(o)) { : missing value where TRUE/FALSE needed R
[英]Error in fzero in R missing value where TRUE/FALSE needed
我正在嘗試通過myfunction通過以下方式在R中使用fzero函數查找冪值:
myfunction = function(delta,data,cv){
if(mean(data^delta)!=0)
y=cv-sd((data^delta),na.rm=TRUE)/mean((data^delta),na.rm=TRUE)
return(y)}
b=repmat(NaN,12,Nest)
for (m in 1:12) {
if (m==1)
indDates=which(is.element(month, c(12, 1, 2)))
else if (m==12)
indDates=which(is.element(month, c(11, 12, 1)))
else
indDates=which(is.element(month, c(m-1, m, m+1)))
cvO=apply(prO[indDates,],2,sd,na.rm=TRUE)/colMeans(prO[indDates,], na.rm=TRUE)
for (i in 1:Nest) {
if (!is.na(cvO[i]))
b[m,i]=fzero(function(x) myfunction(x,abs(prM[indDates,i]),cvO[i]),1)
}
}
但是我收到以下錯誤消息:if(fb == 0)return(list(x = b,fval = fb))中的錯誤:缺少需要TRUE / FALSE的值,我不明白這是怎么回事,我該怎么辦修理它? 有人可以幫我嗎?
最后,我可以通過以下方式解決此問題:
date=DatevecV(t1) % t1 are time values from netCDF file
ndata=nrow(prO)
Nest=ncol(prO)
b=repmat(NaN,12,Nest)
month=unlist(date[2])
varCoeficient = function(delta,data,cv){
y=cv-sd((data^delta),na.rm=TRUE)/mean((data^delta),na.rm=TRUE)
return(y)}
for (m in 1:12) {
if (m==1)
indDates=which(is.element(month, c(12, 1, 2)))
else if (m==12)
indDates=which(is.element(month, c(11, 12, 1)))
else
indDates=which(is.element(month, c(m-1, m, m+1)))
cvO=apply(prO[indDates,],2,sd,na.rm=TRUE)/colMeans(prO[indDates,], na.rm=TRUE)
for (i in 1:Nest) {
if (!is.na(cvO[i])) {
bi <- try(fzero(function(x)
varCoeficient(x,abs(prM[indDates,i]),cvO[i]),1),silent=T)
if ("try-error" %in% class(bi)) { # an error occurred
b[m,i] <- NA
} else {
b[m,i] <- bi$x
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.