The Art of R Programming

(WallPaper) #1
[1,]12345678
[2,]1491625364964

We do get a 2-by-8 matrix, not an 8-by-2 one, but it’s just as useful this
way. We’ll discusssapply()further in Chapter 4.

2.7 NA and NULL Values.......................................................


Readers with a background in other scripting languages may be aware of
“no such animal” values, such as None in Python and undefined in Perl. R
actually has two such values: NA and NULL.
In statistical data sets, we often encounter missing data, which we rep-
resent in R with the value NA. NULL, on the other hand, represents that
the value in question simply doesn’t exist, rather than being existent but
unknown. Let’s see how this comes into play in concrete terms.

2.7.1 Using NA......................................................


In many of R’s statistical functions, we can instruct the function to skip over
any missing values, or NAs. Here is an example:

> x <- c(88,NA,12,168,13)
>x
[1] 88 NA 12 168 13
> mean(x)
[1] NA
> mean(x,na.rm=T)
[1] 70.25
> x <- c(88,NULL,12,168,13)
> mean(x)
[1] 70.25

In the first call,mean()refused to calculate, as one value inxwas NA. But
by setting the optional argumentna.rm(NA remove) to true (T), we calculated
the mean of the remaining elements. But R automatically skipped over the
NULL value, which we’ll look at in the next section.
There are multiple NA values, one for each mode:

> x <- c(5,NA,12)
> mode(x[1])
[1] "numeric"
> mode(x[2])
[1] "numeric"
> y <- c("abc","def",NA)
> mode(y[2])
[1] "character"
> mode(y[3])
[1] "character"
Vectors 43
Free download pdf