缺少值,其中"如果"需要TRUE / FALSE条款

时间:2018-02-14 12:15:35

标签: r loops if-statement max

我想计算我一直在寻找的所有人的最大值(评论的部分是给我错误的部分),但我在控制台中得到错误"缺少值需要TRUE / FALSE"。 我该怎么办?

这是我的代码:

V40D <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
V18D <- c(0, 9, 12, 15, 18, 23, 28, 98, 69, 43, 54, 86, 84, 27, 18, 63, 59, 27, 90)
MAX = -Inf    ## initialize maximum
for (i in V18D) {
  xPos1 <- sum(V40D[V18D <= i] == 1)
  xPos1
  xNeg1 <- sum(V40D[V18D <= i] == 0)
  xNeg1
  NumebrTot1 <- xPos1 + xNeg1
  Max1 <- xPos1 * log(xPos1 / NumebrTot1) + xNeg1 * log(xNeg1 / NumebrTot1)
  xPos2 <- sum(V40D[V18D > i] == 1)
  xPos2
  xNeg2 <- sum(V40D[V18D > i] == 0)
  xNeg2
  NumebrTot2 <- xPos2 + xNeg2
  Max2 <-
    xPos2 * log(xPos2 / NumebrTot2) + xNeg2 * log(xNeg2 / NumebrTot2)
  MaxTotDef = Max1 + Max2
  print(paste(MaxTotDef))
  # if(MaxTotDef != is.na(NA) && MaxTotDef > MAX)
  # MAX < -MaxTotDef
  # print(paste(MAX))
}

2 个答案:

答案 0 :(得分:1)

V40D<- c(0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1)
V18D<- c(0,9,12,15,18,23,28,98,69,43,54,86,84,27,18,63,59,27,90)
MAX = -Inf    ## initialize maximum

for(i in V18D){
  xPos1 <- sum(V40D[V18D<=i]==1)   
  xPos1                         
  xNeg1 <- sum(V40D[V18D<=i]==0)  
  xNeg1                         
  NumebrTot1 <- xPos1 + xNeg1   
  Max1 <- xPos1 * log(xPos1/NumebrTot1) + xNeg1 *log(xNeg1/NumebrTot1)
  xPos2 <- sum(V40D[V18D>i]==1)         
  xPos2                              
  xNeg2 <- sum(V40D[V18D>i]==0)        
  xNeg2                              
  NumebrTot2 <- xPos2+xNeg2          
  Max2 <- xPos2 * log(xPos2/NumebrTot2) + xNeg2 *log(xNeg2/NumebrTot2)
  MaxTotDef= Max1 + Max2 
  if(!is.nan(MaxTotDef) && MaxTotDef > MAX) {
    MAX<-MaxTotDef 
  }
  print(paste(MAX)) 
}

将导致

[1] "-Inf"
[1] "-Inf"
[1] "-Inf"
[1] "-Inf"
[1] "-10.7275102596923"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"
[1] "-9.61883596344456"

如果您只想打印实际结果,请将print放在if - 子句的括号内:

for(i in V18D){
  xPos1 <- sum(V40D[V18D<=i]==1)   
  xPos1                         
  xNeg1 <- sum(V40D[V18D<=i]==0)  
  xNeg1                         
  NumebrTot1 <- xPos1 + xNeg1   
  Max1 <- xPos1 * log(xPos1/NumebrTot1) + xNeg1 *log(xNeg1/NumebrTot1)
  xPos2 <- sum(V40D[V18D>i]==1)         
  xPos2                              
  xNeg2 <- sum(V40D[V18D>i]==0)        
  xNeg2                              
  NumebrTot2 <- xPos2+xNeg2          
  Max2 <- xPos2 * log(xPos2/NumebrTot2) + xNeg2 *log(xNeg2/NumebrTot2)
  MaxTotDef= Max1 + Max2 
  if(!is.nan(MaxTotDef) && MaxTotDef > MAX) {
    MAX<-MaxTotDef 
    print(paste(MAX)) 
  }
}

结果:

[1] "-10.7275102596923"
[1] "-9.61883596344456"

答案 1 :(得分:0)

您所寻找的循环中几乎没有变化?

for(i in V18D){
  i=i+1
  xPos1 <- sum(V40D[V18D<=i]==1)   
  xPos1                         
  xNeg1 <- sum(V40D[V18D<=i]==0)  
  xNeg1                         
  NumebrTot1 <- xPos1 + xNeg1   
  Max1 <- xPos1 * log(xPos1/NumebrTot1) + xNeg1 *log(xNeg1/NumebrTot1)
  xPos2 <- sum(V40D[V18D>i]==1)         
  xPos2                              
  xNeg2 <- sum(V40D[V18D>i]==0)        
  xNeg2                              
  NumebrTot2 <- xPos2+xNeg2          
  Max2 <- xPos2 * log(xPos2/NumebrTot2) + xNeg2 *log(xNeg2/NumebrTot2)
  MaxTotDef= sum(c(Max1, Max2),na.rm=T) 
  #print(paste(MaxTotDef))
  if(!is.na(MaxTotDef)) {
    if (MaxTotDef > MAX){
      MAX<-MaxTotDef
    }
  }
  print(MAX)
}

结果:

[1] -12.36531
[1] -11.5174
[1] -10.58501
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713
[1] -9.547713