-1

I am not sure what is wrong with this script. I get the following error message "Error in if (is.na(MOE)) { : the condition has length > 1"

See the snippet below:

z_score_val <- function(GEOID, X1, MOE) {
  if (is.na(MOE)) {
    0
  } 
  else {
  SE1 <- (MOE/1.645)
  sqrM <- (MOE^2)
  SE2 <- sqrt(sum(sqrM,na.rm = TRUE))/1.645
  X2 <- mean(X1,na.rm = TRUE)
  message(X2, ' this is the mean X2')
  message(SE2, ' this is SE2')
  sign_test <- abs((X1-X2))/sqrt(SE1^2 + SE2^2)
  my_list <- list(GEOID, MOE, SE1,sqrM, sign_test)
  return(my_list) 
  }
  #return(sign_test)
}

I am not sure what else can be wrong

Phil
  • 7,287
  • 3
  • 36
  • 66
  • What are you passing into the function? I don't get an error if I just copy/paste what you provided into R. so it seems it matters what you are passing in and what you expect to get out. – MrFlick Jul 17 '23 at 20:24
  • 1
    You have not provided us with sufficient information to help - consider editing your question to describe what you are trying to achieve and providing example input data and what your desired outcome would be. For tips, see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – jpsmith Jul 17 '23 at 21:15

1 Answers1

0

Try this:

It could be just a if condition issue which is common with R.

z_score_val <- function(MOE) {
if(!(is.na(MOE))){
  print("Hello")
  # Do your else task here
}
  else 0
}

z_score_val(NA)