using multiple arguments (for executions, not conditions) for 'if' function

#1
Hi,
Within a nested for loop, I am trying to do something like

If (p<0.05), then (do A, and do B and do C), else (do nothing)

To give details
if (p<0.05 )

{jpeg(file = paste("Bbl", ".jpeg", sep=""))
IP<-influencePlot(Reg)
IP<-cbind(ID,IP) #ID is for purpose of identification, using ids from the for loops
write.table(IP,paste(dat.loc, "AllIP",".txt", sep=""),sep='\t', col.names = FALSE, append = TRUE)
}
else
{
}

In short, for the chosen dataset in innermost 'for' loop, if the value for p is less than 0.05, I want it to do three things and if not, then do nothing. Can someone suggest how this can be done? (the above code is not working, giving an error 'Error in if (p < 0.05) { : missing value where TRUE/FALSE needed'
 

Dason

Ambassador to the humans
#2
The error seems fairly self explanatory to me. Without your actual full code I can't say for sure but... do you actually have anything stored in p?
 
#3
Hi, Yes, 'p' is extracted as p.value from the regression on the chosen two vectors.
The original code is very long with four for loops that are used to select the data and then name the outputs accordingly. What I have given is what is actually executed within the innermost for loop. I am not sure how to do three different executions (A,B and C) once the if - true/false is determined

The error seems fairly self explanatory to me. Without your actual full code I can't say for sure but... do you actually have anything stored in p?
 

Dason

Ambassador to the humans
#4
Hi, Yes, 'p' is extracted as p.value from the regression on the chosen two vectors.
But are you sure it is what you think it is and isn't empty? The error is pretty explicit. There really isn't anything else that it could be.
I am not sure how to do three different executions (A,B and C) once the if - true/false is determined
Your code is essentially correct. You can't place the braces that close the if and the else on different lines though.
Code:
if(.1 < .5){
  x <- 3
  y <- x + 1
  print("true")
}else{
  print("false")
}
If the else block is just empty then you can just completely omit it.

Like I said the issue is just that your code isn't running because p isn't what you think it is (apparently) and after the comparison to a number p<.05 gives an empty value instead of TRUE or FALSE.
 
#5
Thanks Dason, Thant was very helpful. The problem is not solved, but there is light at the end of the tunnel.

But are you sure it is what you think it is and isn't empty? The error is pretty explicit. There really isn't anything else that it could be.
On close inspection I discovered that some data sets have all zero values. Hence for those data sets p is empty and that is causing some problem.

Your code is essentially correct. You can't place the braces that close the if and the else on different lines though.
Code:
if(.1 < .5){
  x <- 3
  y <- x + 1
  print("true")
}else{
  print("false")
}
Thanks for that. This resolves the issue of the if function in that case. Now I may have to look at the complicated restructuring of for loops as well as figure out how to deal with those all zeros data sets.

If the else block is just empty then you can just completely omit it.

Like I said the issue is just that your code isn't running because p isn't what you think it is (apparently) and after the comparison to a number p<.05 gives an empty value instead of TRUE or FALSE.
The other challenge I have is I need to do 5-6 such diagnostic tests on the data chosen by the for loops. So Would it work well if I just place all those if..else... statements for those 5-6 one below the other inside the innermost loop?

Thanks a lot again!
Cheers