R OR short-circuiting

R thing I didn’t know until today: | doesn’t short circuit || does.

# this will die with an error when some.val is null because it can't compare a null value to n.
is.null(some.val) | (some.val > n)

# but if you use the short circuiting OR then as the first part of the expression is true it doesn't bother to evaluate the second part, so it works fine when some.val is null

is.null(some.val) || (some.val > n)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s