Reputation: 53
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??
Upvotes: 0
Views: 1850
Reputation: 405
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
Upvotes: 1