Martin Martin -4 years ago 140
R Question

Unexpected symbol in "if"

I'm programming (using R) in order to calculate the expected time of life of a system.
It has 2 computers and 1 processor, the systems works when at least 1 computer and the processor works.

Keeping that in mind, i've tried to calculate the expected life by programming the following lines, but i couldn't calculate it because i made an error . Can someone tell me what can i do to fix that error and make the code work? Any idea of why that is happening?.

Thanks a lot!

life<-function(tfailC,tfailP,tfailC1, tfailC2,ttotalfail){
n<-1000
tfailC1<-runif(n, min=0, max=500)
tfailC2<-runif(n, min=0, max=500)
tfailP<-runif(n, min=0, max=1500)
tfailC=0
ttotalfail=0
for (i in 1:n) {

if
tfailC1>tfailC2 #error:Error: unexpected symbol in:"if tfailC1"
tfailC<-tfailC1
else
tfailC<-tfailC2

if tfailC>tfailP
ttotalfail<-tfailC
else
ttotalfail<-tfailP
}
}return(ttotalfail)

Answer Source

Try something like this:

life<-function(tfailP,tfailC1, tfailC2){
  tfailC <- pmax(tfailC1,tfailC2)
  ttotalfail <- pmax(tfailC,tfailP)
  return(ttotalfail)
}

#Test function below
n<-1000
tfailC1<-runif(n, min=0, max=500)
tfailC2<-runif(n, min=0, max=500)
tfailP<-runif(n, min=0, max=1500)

life(tfailP,tfailC1,tfailC2)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download