0
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??

NEXUSAG
  • 53
  • 8

1 Answers1

1

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
bluefish
  • 395
  • 2
  • 13
  • Thank you for helping out. I tried with the above script.. as my data has NA values ... I got a error saying "missing values are not allowed in subscripted assignments of data frames" – NEXUSAG Dec 03 '15 at 02:46