vendredi 26 juillet 2019

How to Create a function that will return the sum of 2 integers?

I've encountered a simple question but failed to write the code. Grateful if you can help me.

The question: Create a function that will return the sum of 2 integers.

My code:

q1 <- function(a, b) {
  if (a == as.integer(a)) {
    if (b == as.integer(b)) {
      result1 <- a + b
    }
  }
} else {
  result1 <- NA
  result1
}

q1(3,6) - suppose it returns 9

q1(3.1,6) - suppose it returns NA

BTW, why can I not write the syntax in this way? if(is.integer(a) = TRUE){

Aucun commentaire:

Enregistrer un commentaire