watchtower
watchtower

Reputation: 4298

NULL type of object in R

I am still a novice user in R, and have been reading Advanced R by Hadley to improve my R programming skills.

I came across this code in his book:

NULL>0 

The output for this code is logical(0).

I have two questions on this:

Question 1: What does logical(0) mean?

Question 2: I would have expected a TRUE/FALSE as the return value. This is because he talks about the rules for coercion in R, specifically that Logical < Integer < Double < Character (Least flexible to most flexible). Hence, I assume that NULL is of type Logical.

I am not really sure where NULL fits into this equation. I'd appreciate any explanation.

Thanks in advance.

Upvotes: 3

Views: 1645

Answers (1)

mpjdem
mpjdem

Reputation: 1544

logical(0) is a logical vector of length zero. NULL is its own type, as typeof(NULL) shows, and does not contain anything, as length(NULL) shows.

It does not make sense to compare NULL to 0; it is not a numeric value, or even a value at all. There is no answer possible to the comparison, and therefore no logical value is returned, only an empty vector.

Consider for instance the output of:

c(TRUE, FALSE, NULL)

The NULL is ignored, rather than yielding an error or being converted. You should consider it as being literally nothing at all, contrary to NA which is an indication of a missing value of a certain type - try replacing NULL with NA in the above concatenation.

Upvotes: 4

Related Questions