8

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.

InfiniteFlash
  • 1,038
  • 1
  • 10
  • 22

1 Answers1

18

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
Psidom
  • 209,562
  • 33
  • 339
  • 356