I have problem with Boolean operators &&

I have a problem in this code when I want to compare the sum of x with s1 and sum y with s2 I got the NA. I storage the sum of x in xx and the sum of y in yy then compear that with s1 and s2 by using && operators why I got NA could someone help me to correct this problem. Thank you.

rm(list = ls())
options(scipen=999) 

exactpois <- data.frame(X = numeric(0),
                        Y = numeric(0))
lambda=4
B1=2
B2=4
set.seed(0)

for(s1 in seq(from = 1.5, to = 7.5, by = 2)) 
{
  for(s2 in seq(from = 1.5, to = 13.5, by = 4)) 
  {
    count=0
    l=0
    xx=0
    yy=0
    for (j in 1:100) 
    {
      f = rpois(1,lambda)
      if(f == 0)
      {
        count = count + 1
        next
      }
      
      x = rexp(f,1/B1)
      y = rexp(f,1/B2)
      
      data_a1 <-exactpois[1:50,]
      xx=sum(data_a1$X)
      yy=sum(data_a1$Y)
      
      if(xx== s1 && yy<= s2)
      {
        l = l + 1
      }
      if(xx< s1 && yy== s2)
      {
        l = l + 1
      }
      if(xx< s1 && yy< s2)
      {
        count=count+1
      }
      newrow <- data.frame(X = x, 
                           Y = y)
      exactpois <- rbind(exactpois, newrow)
    }
    p2=(count+l)/10
  
    rho=cor(data_a1$X,data_a1$Y)
    z=pbivnorm(s1,s2,rho)
 
    print(paste0("s1 =",s1))
    print(paste0("s2 =",s2))
    print(paste0("The true p-value =",p2))
    print(paste0("The Normal =",z))
  }
}

I am a beginner in Rstudio could you help me please and correct my code because I couldn't do what you mentioned. Thanks in Advance.

The problem is basically as @Yarnabrina describes. You create xx=sum(data_a1$X), but at this point data_a1$X hasn't been defined. So the sum is NA. You need to change the logic of your code so that either data_a1$X is defined or so that you check for is.na(xx) before trying the if statement.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.