PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] How do I make my custom function return an error message if one of the vector elements has NA or is not an integer in R?

 April 28, 2022     function, integer, na, r, warnings     No comments   

Issue

What I want to do is a function where x is a vector, and y any integer. If y is inside the vector x, then it should return "TRUE". Also, if the vector contains NAs or decimals, then it should print an error message.

So far I have created this, but if I input search(c(9,8,3,NA),3) it gives me this message:

Warning message:
In if (x%%1 != 0 | anyNA(x) == TRUE) { :
  the condition has length > 1 and only the first element will be used

If if input a vector with a decimal in it like this search(c(8,9,7.01,12),12)it won't give an ERROR message.

This is my code so far:

search <- function(x,y){
  if (x%%1!=0 | anyNA(x)==TRUE){
    print("ERROR")
  }else{
    if(y %in% x){
      print(TRUE)
    }
    else
      print(FALSE)
  }
}

Solution

If you want your function to produce an error, use stop, not print. Any program that relies on the output of the function will otherwise keep running, without noticing anything is wrong. This could make things very hard to debug later. stop throws an error, which can then be handled appropriately. Also, because the function will exit if the condition is met, you don't need an else afterwards: that code will only ever run if the condition isn't met, so the else is redundant.

You can also simplify some of the logic. You don't need if(condition == TRUE), since if(condition) does the same thing. Finally, the construction if(condition){ print(TRUE) } else { print(FALSE) } is logically identical to print(condition)

search <- function(x, y){
  if (any(x %% 1 != 0) | anyNA(x) | length(y) != 1) stop("Error")
  y %in% x
}

Now try it on test cases:

search(c(1, 3, 5), 3)
#> [1] TRUE
search(c(1, 3, 5), 2)
#> [1] FALSE
search(c(1, 3, NA), 3)
#> Error in search(c(1, 3, NA), 3): Error
search(c(1, 3, 5.1), 3)
#> Error in search(c(1, 3, 5.1), 3): Error
search(c(1, 3, 5), c(1, 3))
#> Error in search(c(1, 3, 5), c(1, 3)): Error

Created on 2020-05-15 by the reprex package (v0.3.0)



Answered By - Allan Cameron
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing