Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if value == integer(0) in R [duplicate]

Tags:

r

vector

I'm using the grep function in R to check if a conditional regular expression is met.

What I have is this: grep(expression, string) where an example might be

   value=  grep("\\s[A-z]", "  ")
   value

which outputs

integer(0)

What I want to be able to do is check is if value == integer(0) and

return TRUE if value is integer(0)

return FALSE if value is not integer(0)

Is there a way to do this in R? If there are alternatives, I am open to them. For example, grep might have an option to output the result as a logical value,

TRUE and FALSE, or

0 and 1 or something related.

like image 740
InfiniteFlash Avatar asked Mar 05 '17 01:03

InfiniteFlash


1 Answers

You can use identical which is the safe and reliable way to test two objects for being exactly equal (from the docs):

value = integer(0)

identical(value, integer(0))
# [1] TRUE

Or do the following check:

is.integer(value) && length(value) == 0
# [1] TRUE
like image 66
Psidom Avatar answered Sep 21 '22 20:09

Psidom