"中的意外符号,如果"

时间:2017-02-28 15:35:45

标签: r

我编程(使用R)以计算系统的预期寿命。 它有2台计算机和1个处理器,系统工作时至少有1台计算机和处理器工作。

记住这一点,我试图通过编程以下几行计算预期寿命,但我无法计算它,因为我犯了一个错误。有人可以告诉我,我该怎么做才能解决该错误并使代码有效?知道为什么会这样吗?。

非常感谢!

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)

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

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)

答案 1 :(得分:0)

感谢您的帮助!如果有人对此感兴趣,请查看实际有效的代码:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Border ClipToBounds="True">
        <Ellipse Stroke="Black" StrokeThickness="1" RenderTransformOrigin="0,1">
            <Ellipse.RenderTransform>
                <ScaleTransform ScaleX="2" ScaleY="2"/>
            </Ellipse.RenderTransform>
        </Ellipse>
    </Border>
</Grid>