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

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

r - the condition has length > 1 and only the first element will be used in if else statement

pred=data.frame(seq(1,100,by=2))
obs=data.frame(seq(1,100,by=3))
pr=data.frame(seq(1,200,by=4))
for (i in 1:dim(pred)[2]) {
  if (pr[,i]>max(pred[,i],na.rm=T)){
    pr[,i]=pr[,i]*(max(obs[,i],na.rm=T)/max(pred[,i],na.rm=T))}}

I am getting error"the condition has length > 1 and only the first element will be used" any alternatives??

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

If I understand you correctly you want to multiply all values matching a condition with a factor. For your example it would be:

pred=data.frame(seq(1,100,by=2))
obs=data.frame(seq(1,100,by=3))
pr=data.frame(seq(1,200,by=4))
condition <- pr[,1] > max(pred[,1],na.rm=T)
#set NA values false
condition[is.na(condition)] <- F
fac <- max(obs[,1],na.rm=T)/max(pred[,1],na.rm=T)
pr[condition,1] <- pr[condition,1]*fac
#do other stuff if the condition is false

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...