Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 21 / 68

Writing NULL-safe code

This lesson covers handling values safely against NULL in R, with the aim of preventing errors when a value has not been set.

R represents "no value has been set" with NULL. The is.null() function tells you whether a value is NULL.

The sample code declares address <- NULL, tests it with is.null(address), and shows an alternative message when it is NULL. When a value is set, that value is used instead.

A common early stumble is the difference between NULL and NA. R has two similar-looking concepts — NULL for "no value exists" and NA for "missing value" — and they need to be used distinctly.

In professional work, handling missing data comes up constantly in statistics, so getting NULL and NA right directly affects how much you can trust your code.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run R directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)