The Art of R Programming

(WallPaper) #1

2.7.2 Using NULL....................................................


One use of NULL is to build up vectors in loops, in which each iteration
adds another element to the vector. In this simple example, we build up a
vector of even numbers:

# build up a vector of the even numbers in 1:10
> z <- NULL
> for (i in 1:10) if (i %%2 == 0) z <- c(z,i)
>z
[1]246810

Recall from Chapter 1 that%%is the modulo operator, giving remainders
upon division. For example,13 %% 4is 1, as the remainder of dividing 13 by
4 is 1. (See Section 7.2 for a list of arithmetic and logic operators.) Thus the
example loop starts with a NULL vector and then adds the element 2 to it,
then 4, and so on.
This is a very artificial example, of course, and there are much better
ways to do this particular task. Here are two more ways another way to find
even numbers in 1:10:

> seq(2,10,2)
[1]246810
>2*1:5
[1]246810

But the point here is to demonstrate the difference between NA and
NULL. If we were to use NA instead of NULL in the preceding example, we
would pick up an unwanted NA:

>z<-NA
> for (i in 1:10) if (i %%2 == 0) z <- c(z,i)
>z
[1]NA246810

NULL values really are counted as nonexistent, as you can see here:

> u <- NULL
> length(u)
[1] 0
>v<-NA
> length(v)
[1] 1

NULL is a special R object with no mode.

44 Chapter 2

Free download pdf